一. Stream流去重
// 创建一个包含DeptEntity对象的List,并向其中添加若干元素
List<DeptEntity> deptEntityList= new ArrayList<>();
deptEntityList.add(new DeptEntity(1, '部门1'));
deptEntityList.add(new DeptEntity(1, '部门1'));
deptEntityList.add(new DeptEntity(2, '部门2'));
// 使用java8的stream流进行元素去重(根据DeptEntity中的id进行元素去重)
ArrayList<DeptEntity> disList = deptEntityList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(DeptEntity::getId))
)
, ArrayList::new
)
);
二. TreeSet去重
List<DeptEntity> deptEntityList= new ArrayList<>();
deptEntity