java list 对象根据某个字段去重的方法
List<User> lists = new ArrayList<>();
for (int i = 0; i < 40000; i++) {
if (i%2 == 0){
lists.add(new User("aa1",1));
}else {
lists.add(new User("aa2",2));
}
}
lists = lists.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(User::getAge))), ArrayList::new));
for (int i = 0; i < lists.size(); i++) {
System.out.println(lists.get(i).getName());
}