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 集合中 元素 出現(xiàn)次數(shù) 大于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); }
}?
結(jié)果:
1
6