Person p1 = new Person();
p1.setId(1);
p1.setName("11");
Person p2 = new Person();
p2.setId(1);
p2.setName("22");
Person p3 = new Person();
p3.setId(8);
p3.setName("88");
List<Person> persons = Arrays.asList(p1,p2,p3);
System.out.println("去重前---------------------------------");
persons.forEach(e -> System.out.println(e.getId()));
List<Person> unique = persons.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new)
);
System.out.println("去重後---------------------------------");
unique.forEach(p -> System.out.println(p.getId()));
Java 泛型集合根據對象某一屬性去重
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.