zoukankan      html  css  js  c++  java
  • 两个List集合取交集、并集、差集

      list1.removeAll(list2):从list1中移除存在list2中的元素。
      调用流程:removeAll->contains->equals方法,对于引用类型,要使用removeAll,需要重写equals方法

      removeAll源码:

    public boolean removeAll(Collection<?> c) {
            Objects.requireNonNull(c);
            boolean modified = false;
            Iterator<?> it = iterator();
            while (it.hasNext()) {
                if (c.contains(it.next())) {
                    it.remove();
                    modified = true;
                }
            }
            return modified;
    }

      contains源码

    public boolean contains(Object o) {
            Iterator<E> it = iterator();
            if (o==null) {
                while (it.hasNext())
                    if (it.next()==null)
                        return true;
            } else {
                while (it.hasNext())
                    if (o.equals(it.next()))
                        return true;
            }
            return false;
    }

      当对象o不为空时,迭代判断用到了Object的equals方法,而Object的equals方法指的是两个对象的引用是否相等,如果我们要判断两个对象的内容相等,这里就需要重写equals方法。

      JDK1.8 lambda表达式取交集、并集、差集(String类型,已重写了equals方法)

    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("5");
        list1.add("6");
     
        List<String> list2 = new ArrayList<String>();
        list2.add("2");
        list2.add("3");
        list2.add("7");
        list2.add("8");
     
        // 交集
        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);

    // 并集 List<String> listAll = list1.parallelStream().collect(toList()); List<String> listAll2 = list2.parallelStream().collect(toList()); listAll.addAll(listAll2); System.out.println("---并集 listAll---"); listAll.parallelStream().forEachOrdered(System.out :: println); // 去重并集 List<String> listAllDistinct = listAll.stream().distinct().collect(toList()); System.out.println("---得到去重并集 listAllDistinct---"); listAllDistinct.parallelStream().forEachOrdered(System.out :: println); System.out.println("---原来的List1---"); list1.parallelStream().forEachOrdered(System.out :: println); System.out.println("---原来的List2---"); list2.parallelStream().forEachOrdered(System.out :: println); }


  • 相关阅读:
    适配器模式(2)
    设计模式之6大设计原则(1)
    Mybatis框架基础支持层——反射工具箱之MetaClass(7)
    Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)
    Mybatis框架基础支持层——反射工具箱之对象工厂ObjectFactory&DefaultObjectFactory(5)
    Mybatis框架基础支持层——反射工具箱之泛型解析工具TypeParameterResolver(4)
    Guava动态调用方法
    数据库的数据同步
    springboot(二十二)-sharding-jdbc-读写分离
    springboot(二十一)-集成memcached
  • 原文地址:https://www.cnblogs.com/lyrb/p/12923615.html
Copyright © 2011-2022 走看看