public static List<String> getDuplicateElements(List<Dept> list, boolean flag) {
return list.stream() //
.map(e -> { // 获取deptCode或deptAlias的Stream
return flag ? e.getDeptCode() : e.getDeptName();
}).collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b)) // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
.entrySet().stream() // 所有 entry 对应的 Stream
.filter(entry -> entry.getValue() > 1) // 过滤出元素出现次数大于 1 的 entry
.map(entry -> entry.getKey()) // 获得 entry 的键(重复元素)对应的 Stream
.collect(Collectors.toList()); // 转化为 List
}
检查list中是否有重复的元素并返回重复元素
最新推荐文章于 2023-08-12 13:39:48 发布