list集合根据某个属性去重

//使用LinkedHashSet去重
list = new ArrayList<Integer>(new LinkedHashSet<>(list));
//使用HashSet去重
HashSet hashSet = new HashSet(list);
list.clear();
list.addAll(hashSet);
//使用TreeSet去重
TreeSet treeSet = new TreeSet(list);
list.clear();
list.addAll(treeSet);
//使用java8的stream去重
list = list.stream().distinct().collect(Collectors.toList());
//根据某个属性去重
list = list.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(i -> i.getName()))), ArrayList::new));
posted @ 2022-01-03 16:31  一隅桥畔  阅读(77)  评论(0)    收藏  举报