一. Stream流去重
List<DeptEntity> deptEntityList= new ArrayList<>();
deptEntityList.add(new DeptEntity(1, '部门1'));
deptEntityList.add(new DeptEntity(1, '部门1'));
deptEntityList.add(new DeptEntity(2, '部门2'));
ArrayList<DeptEntity> disList = deptEntityList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(DeptEntity::getId))
)
, ArrayList::new
)
);
二. TreeSet去重
List<DeptEntity> deptEntityList= new ArrayList<>();
deptEntityList.add(new DeptEntity(1, "部门1"));
deptEntityList.add(new DeptEntity(1, "部门1"));
deptEntityList.add(new DeptEntity(2, "部门2"));
TreeSet<DeptEntity> deptEntityTreeSet = new TreeSet<>(Comparator.comparing(DeptEntity::getId));
deptEntityTreeSet.addAll(deptEntityList);
List<DeptEntity> arrayList2 = new ArrayList<>(deptEntityTreeSet);
System.out.println(arrayList2);