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中是否有重複的元素並返回重複元素
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.