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


  • 相关阅读:
    Android自定义属性,format详解
    设置EditText是否可编辑
    Android中Application设置全局变量以及传值
    android:ellipsize属性的含义
    Java集合框架List,Map,Set等全面介绍
    二十款漂亮的CSS字体样式,让你受用非浅
    IE浏览器不见了
    display属性值:
    用CSS设置多个背景、背景渐变、指定背景大小
    [转]IE6双倍边距——IE布局BUG集锦
  • 原文地址:https://www.cnblogs.com/lyrb/p/12923615.html