java8 stream 獲取list中 元素 出現次數 大于1的 元素

list.stream().collect(Collectors.toMap(Function.identity(), s -> 1, Integer::sum)) .entrySet() .stream() .filter(entry -> entry.getValue() > 1) .map(Map.Entry::getKey) .collect(Collectors.toList());

java8?stream 獲取list 集合中 元素 出現次數 大于1的 元素

@Test public void test10() {

List<Integer> list = new ArrayList<>();

list.add(1); list.add(2); list.add(1); list.add(1); list.add(6); list.add(6); list.add(7);

List<Integer> collect = list.stream().collect(Collectors.toMap(Function.identity(), s -> 1, Integer::sum)) .entrySet() .stream() .filter(entry -> entry.getValue() > 1) .map(Map.Entry::getKey) .collect(Collectors.toList());

for (Integer integer : collect)

{ System.out.println(integer); }

}?

結果:

1

6

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容