在bug中写bug

导航

 
1.对象集合:
//单独String集合
List<Student> list = new ArrayList<>();
list.add(Student.builder().id("1").name("1").age("1").build());
        list.add(Student.builder().id("2").name("3").age("1").build());
list.add(Student.builder().id("1").name("1").age("1").build());
list.add(Student.builder().id("12").name("1").build());

List<Student> collect = 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(collect);

 

 



2.String集合:
//单独String集合
List<String> list = Arrays.asList("a","b","a","c","d","b");
List<String> collect = list.stream().filter(i -> i != "")               // list 对应的 Stream 并过滤""
        .collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
        .entrySet()
        .stream()                       // 所有 entry 对应的 Stream
        .filter(e -> e.getValue() > 1)         // 过滤出元素出现次数大于 1 (重复元素)的 entry
        .map(Map.Entry::getKey)                // 获得 entry 的键(重复元素)对应的 Stream
        .collect(Collectors.toList());
System.out.println(collect);

 



posted on 2023-03-08 10:50  在bug中写bug  阅读(51)  评论(0编辑  收藏  举报