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 @ 2023-02-06 16:12  黄橙  阅读(980)  评论(0)    收藏  举报