public class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
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 class Test {
public static void main(String[] args) {
Person p1 = new Person(1, "11");
Person p2 = new Person(1, "22");
Person p3 = new Person(2, "11");
List persons = Arrays.asList(p1,p2,p3);
List unique = persons.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new)
);
unique.forEach(p -> System.out.println(p.getId()));
}
}