java8 list取出重复值

@Test
public void test10(){
//根据device_code去重,取出重复值
	List<Employee> emps =new ArrayList<>();
			List<String> dupList = emps.stream().collect(Collectors.groupingBy(Employee::getName, Collectors.counting()))
			.entrySet().stream().filter(e -> e.getValue() > 1)
			.map(Map.Entry::getKey).collect(Collectors.toList());
	System.out.println(dupList);
}
@Test
public void test11(){
	//字符串取出重复值
	List<String> list = new ArrayList<>();
	List<String> repeatList = list.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()))
			.entrySet().stream().filter(e -> e.getValue() > 1)
			.map(Map.Entry::getKey).collect(Collectors.toList());
	System.out.println(repeatList);
}
posted @ 2022-07-12 16:39    阅读(70)  评论(0编辑  收藏  举报