Hard Worker
Hard Worker

Reputation: 1111

How to remove duplicate from list of object in java using stream API

I have searched for a solution for how to remove duplicates from a list with Stream API

found only this question How to remove duplicates from list of objects by id

I have a list of Person i need to filter by the person name, tried with the below snippet but it doesn't filter by the name

private static Map<Person, Integer> getUniqueByBiggerId(Collection<Person> persons) {
        return persons.stream().collect(Collectors.toMap(
                persons ->
                        persons,
                Person::getId,
                (id1, id2) -> {
                    if (id2 > id1)
                        return id2;
                    else
                        return id1;
                }
        ));
    }

public  static void main(String args[]){
   //.........
    List<Person> Persons
            = Arrays.asList(Person1, Person2,Person2,Person1,Person2, Person1);

    getUniqueByBiggerId(Persons);

}

Upvotes: 1

Views: 1778

Answers (3)

alex87
alex87

Reputation: 549

Assuming you have a Person class which has fields id and name and the field id is the field you want to filter by you just need to override equals and hashCode methods:

public class Person {

    private final int id;

    private final String name;

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return id == person.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

Then you can just create a List:

Person personOne = new Person(1, "Name1");
Person personTwo = new Person(2, "Name2");
Person personThree = new Person(1, "Name3");
List<Person> people = Arrays.asList(personOne, personTwo, personThree);

And to eliminate duplicates just write:

List<Person> collect = people.stream().distinct().collect(Collectors.toList());

System.out.println(collect.size());

That will produce the output 2.

Upvotes: 0

the Hutt
the Hutt

Reputation: 18398

You were very close to the solution:

public class Test {
    public static void main(String[] args) throws Exception {
        List<Person> persons = Arrays.asList(new Person("a", 2), new Person("b", 1), new Person("a", 3));
        persons = getUniqueByBiggerId(persons);
        System.out.println(persons);

    }

    private static List<Person> getUniqueByBiggerId(Collection<Person> persons) {
        return new ArrayList<>(
                persons.stream().collect(Collectors.toMap(Person::getName, Function.identity(), (p1, p2) -> {
                    if (p1.getId() > p2.getId())
                        return p1;
                    else
                        return p2;
                })).values());
    }
}

record Person(String name, int id) {
    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}

Output:

[Person[name=a, id=3], Person[name=b, id=1]]

Upvotes: 1

Hard Worker
Hard Worker

Reputation: 1111

private static Map<String, Person> getUniqueByBiggerId(Collection<Person> person) {
        return harmonizedDimensions.stream().collect(Collectors.toMap(()
                person ->
                person.getName(),
                Function.identity(),
                (id1, id2) -> {
                    if (id2.getId() > id1.getId())
                        return id2;
                    else
                        return id1;
                }
        )).values();
    }

Upvotes: 0

Related Questions