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());
@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);
}
}
结果: