zoukankan      html  css  js  c++  java
  • lambda表达式不同对象根据对象某个属性去重

    1.有时候有两个list对象,我们想要去重,比如:

    List<User> userList和List<Person>personList

    想通过User的id和Person的id进行去重,去掉userList中的User的id不等于personList中的Person的id的对象。

    List<User> userList= userList.stream()
                .filter(user-> !personList.stream()
                    .map(person ->person.getId())
                    .collect(Collectors.toList())
                    .contains(user.getId()))
              //  .filter(UniqueUtils.distinctByKey(person ->person.getId()))
             //   .peek(person  -> person .setId(UUIDUtil.uuid()))
                .collect(Collectors.toList());

    2.list 交集/并集/差集/去重并集

         // 交集
            List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
            System.out.println("---得到交集 intersection---");
            intersection.parallelStream().forEach(System.out :: println);
    
            // 差集 (list1 - list2)
            List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
            System.out.println("---得到差集 reduce1 (list1 - list2)---");
            reduce1.parallelStream().forEach(System.out :: println);
    
            // 差集 (list2 - list1)
            List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
            System.out.println("---得到差集 reduce2 (list2 - list1)---");
            reduce2.parallelStream().forEach(System.out :: println);
    
            // 并集
            List<String> listAll = list1.parallelStream().collect(toList());
            List<String> listAll2 = list2.parallelStream().collect(toList());

          listAll.addAll(listAll2);
          System.out.println("---得到并集 listAll---");
          listAll.parallelStream().forEach(System.out :: println);

          // 去重并集
          List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
          System.out.println("---得到去重并集 listAllDistinct---");
          listAllDistinct.parallelStream().forEach(System.out :: println);

          System.out.println("---原来的List1---");
          list1.parallelStream().forEach(System.out :: println);
          System.out.println("---原来的List2---");
          list2.parallelStream().forEach(System.out :: println);

     3.stream初试,map排序,list去重,统计重复元素个数,获取map的key集合和value集合

    //定义一个100元素的集合,包含A-Z
    List<String> list = new LinkedList<>();
    for (int i =0;i<100;i++){
        list.add(String.valueOf((char)('A'+Math.random()*('Z'-'A'+1))));
    }
    System.out.println(list);
    //统计集合重复元素出现次数,并且去重返回hashmap
    Map<String, Long> map = list.stream().
        collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
    System.out.println(map);
    //由于hashmap无序,所以在排序放入LinkedHashMap里(key升序)
    Map<String, Long> sortMap = new LinkedHashMap<>();
    map.entrySet().stream().sorted(Map.Entry.comparingByKey()).
        forEachOrdered(e -> sortMap.put(e.getKey(), e.getValue()));
    System.out.println(sortMap);
    //获取排序后map的key集合
    List<String> keys = new LinkedList<>();
    sortMap.entrySet().stream().forEachOrdered(e -> keys.add(e.getKey()));
    System.out.println(keys);
    //获取排序后map的value集合
    List<Long> values = new LinkedList<>();
    sortMap.entrySet().stream().forEachOrdered(e -> values.add(e.getValue()));
    System.out.println(values);
    --------------------- 
  • 相关阅读:
    C语言 简单的队列(数组队列)
    C语言 复杂的栈(链表栈)
    C语言 简单的栈
    C语言 链表的使用(链表的增删查改,链表逆转,链表排序)
    C语言 常用的时间函数
    C语言 百炼成钢17
    C语言 const常量讲解
    ndroid如何监听开机广播和关机广播
    Android判断GPS是否开启和强制帮用户打开GPS
    android java.lang.IllegalArgumentException: Comparison method violates its general contract! 问题
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/9859144.html
Copyright © 2011-2022 走看看