首页 > 编程语言 >java8 list取出重复值

java8 list取出重复值

时间:2023-02-06 16:24:05浏览次数:43  
标签:Collectors stream 重复 list List collect java8

@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);
}

 

标签:Collectors,stream,重复,list,List,collect,java8
From: https://www.cnblogs.com/RedOrange/p/17091626.html

相关文章