HStoltz
HStoltz

Reputation: 183

Printing distinct elements from a List

I created an ArrayList which contains the ID, name and age of different people. I'm supposed to print only the different names on the console, but I see many examples where the list is a String or a int list, but didn't find anything with lists that contain more than 1 piece of information like in my case. How can I print only the distinct names? (i.e. on the code below, I want to print only Paul, Sabrina and Max)

Main class

public class Main {
    public static void main(String[] args) {
        Person p1 = new Person(1, "Paul", 23);
        Person p2 = new Person(2, "Sabrina", 28);
        Person p3 = new Person(3, "Paul", 51);
        Person p4 = new Person(4, "Max", 34);
        Person p5 = new Person(5, "Paul", 31);

        ArrayList<Person> people = new ArrayList<>();
        people.add(p1);
        people.add(p2);
        people.add(p3);
        people.add(p4);
        people.add(p5);
    }
}

Person Class

public class Person {
    private int id;
    private String name;
    private int age;

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Id: " + this.id
                + ", Name: " + this.name
                + ", Age:" + this.age + "\n";
    }
}

Upvotes: 1

Views: 331

Answers (3)

user15402945
user15402945

Reputation:

You can use Stream#distinct method for this purpose:

people.stream()
        // take person's name
        .map(Person::getName)
        // only distinct names
        .distinct()
        // output line by line
        .forEach(System.out::println);

Output:

Paul
Sabrina
Max

Upvotes: 1

dreamcrash
dreamcrash

Reputation: 51513

The simplest solution is to use a Set to check if the Person that you are about to print was already print it before or not:

Set<String> temp = new HashSet<>();
for(Person p : people) {
    // True if was not printed before.
    if (temp.add(p.getName())) {
        System.out.println(p.getName());
    }
}

With Streams you can do it as follows:

people.stream()
      .map(Person::getName)
      .collect(Collectors.toSet())
      .forEach(System.out::println);

Upvotes: 3

Henry Twist
Henry Twist

Reputation: 5980

There are so many ways to do this, but they all involve checking the names against each other for duplicates.

Probably the nicest way would be to add all the names to a Set and then print them out from there. Something like:

Set<String> distinctNames = new HashSet<>();

for (Person p : people) {
    
    distinctNames.add(p.getName());
}

for (String name : distinctNames) {

    ...
}

There is some good documentation here on the concept of a Set, but essentially it is just a collection of unique elements.

Upvotes: 1

Related Questions