7

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);

}
}
akhil_mittal
  • 21,313
  • 7
  • 90
  • 91
Venkata Palakolanu
  • 127
  • 1
  • 1
  • 4
  • 1
    look at https://stackoverflow.com/questions/29670116/remove-duplicates-from-a-list-of-objects-based-on-property-in-java-8 – Adrian Jul 31 '17 at 14:35
  • override equals & hashcode methods in Person class for duplication removal using Set – Noman Khan May 29 '18 at 17:03
  • https://stackoverflow.com/questions/23699371/java-8-distinct-by-property[enter link description here](https://stackoverflow.com/questions/23699371/java-8-distinct-by-property) – 取一个好的名字 May 09 '19 at 07:30

4 Answers4

35

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()));
Eugene
  • 110,516
  • 12
  • 173
  • 277
30
List<Person> personsWithoutDuplicates = persons.stream()
 .distinct()
 .collect(Collectors.toList());
Taras Melnyk
  • 2,729
  • 3
  • 34
  • 32
1
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

Timbus Calin
  • 11,679
  • 3
  • 35
  • 51
Rohit
  • 331
  • 1
  • 5
  • 15
-3
public static void main(String[] args) {

    List<Integer> arrList = Arrays.asList(10, 20, 10, 30, 20, 40, 50, 40);

    arrList.stream().distinct().collect(Collectors.toList()).forEach(System.out::println);

}
  • How is this answer different from the one posted by Taras Melnyk years ago? Also note, that op uses Person instances, not integers. – BDL Jan 27 '20 at 11:39
  • My Answer is not belonging to others i am doing the distinct value operations using Integer class as Type Cast – Manash Ranjan Dakua Feb 20 '20 at 10:42