9

I have a class below, and wanted to remove duplicate person which contain same name, how to do by using Java8 Lambda, expected List contains p1, p3 from the below.

Person:

public class Person {

public int id;
public String name;
public String city;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

}

Testing:

import java.util.ArrayList;
import java.util.List;

public class Testing {

public static void main(String[] args) {

    List<Person> persons = new ArrayList<>();

    Person p1 = new Person();
    p1.setId(1);
    p1.setName("Venkat");
    p1.setCity("Bangalore");
    Person p2 = new Person();

    p2.setId(2);
    p2.setName("Venkat");
    p2.setCity("Bangalore");

    Person p3 = new Person();
    p3.setId(3);
    p3.setName("Kumar");
    p3.setCity("Chennai");

    persons.add(p1);
    persons.add(p2);
    persons.add(p3);

}
}
3

3 Answers 3

37

You could filter them out and generate a unique Set:

Set<Person> set = persons.stream()
            .collect(Collectors.toCollection(() -> 
                 new TreeSet<>(Comparator.comparing(Person::getName))));

Or even nicer:

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

persons.removeIf(p -> !namesAlreadySeen.add(p.getName()));
Sign up to request clarification or add additional context in comments.

5 Comments

ooh, you faster than me, +1! you should tell the OP to make the persons to List<Person> rather than List<Object>.
You actually don't need a stream for this, do you? Just create the TreeSet with the comparator and then use addAll.
No need for block syntax here, you can use the compact expression syntax just like here.
@FedericoPeraltaSchaffner you're right, I don't have to use streams... thx
Helped me with some other kind of problem :p
36

The distinct() function can be used to removed duplicates using a natural comparison, for that first override equals() and hashCode() method in Person class.

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

Note: for custom comparison Comparator interface can be used.

3 Comments

You sure that's going to work if the class does override equals and hash?
@RobertMoskal: the question is, rather, why you'd want to sneak around implementing equals and hashCode in the first place.
distinct - Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
0
List<Person> modified = pesrons.stream().collect(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(Person::getName)))).stream().collect(Collectors.toList());

This will return a list of non duplicates based on Name.

You can refer this also Remove duplicates from a list of objects based on property in Java 8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.