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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容