使用lambda表达式取出list中重复的、不重复的数据
原数据:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]
不重复的数据[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
重复的数据[1, 2, 3, 4]
Process finished with exit code 0
@Test
public void compare(){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
list.add(1);
list.add(2);
list.add(3);
list.add(4);
List<Integer> newList = list.stream().distinct().collect(Collectors.toList());
System.out.println("不重复的数据:"+newList);
Map<Integer, Long> countMap = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<Integer> compareList = countMap.keySet().stream().filter(key -> countMap.get(key) > 1).distinct().collect(Collectors.toList());
System.out.println("重复的数据:"+compareList.toString());
}