用list实现集合的操作,交集,并集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c_he_n/article/details/80316839

用list实现集合的操作,交集,并集等。

这里写图片描述

图示从网上下载的

通过arraylist分别实现A,B,A+C这个集合。

/**
    * Removes from this list all of its elements that are contained in the
    * specified collection.
    *
    * @param c collection containing elements to be removed from this list
    * @return {@code true} if this list changed as a result of the call
    * @throws ClassCastException if the class of an element of this list
    *         is incompatible with the specified collection
    * (<a href="Collection.html#optional-restrictions">optional</a>)
    * @throws NullPointerException if this list contains a null element and the
    *         specified collection does not permit null elements
    * (<a href="Collection.html#optional-restrictions">optional</a>),
    *         or if the specified collection is null
    * @see Collection#contains(Object)
    */
   public boolean removeAll(Collection<?> c) {
       Objects.requireNonNull(c);
       return batchRemove(c, false);
   }
/**
     * Retains only the elements in this list that are contained in the
     * specified collection.  In other words, removes from this list all
     * of its elements that are not contained in the specified collection.
     *
     * @param c collection containing elements to be retained in this list
     * @return {@code true} if this list changed as a result of the call
     * @throws ClassCastException if the class of an element of this list
     *         is incompatible with the specified collection
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if this list contains a null element and the
     *         specified collection does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>),
     *         or if the specified collection is null
     * @see Collection#contains(Object)
     */
    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }

其他的方法经常用到,这个两个反而用的很少,所以只把这个两个的源码拿出来,并实现简单集合操作。
public boolean removeAll(Collection

public class TestList {
    public static void main(String args[]) {
        ArrayList<String> test = new ArrayList<>();
        test.add("1");
        test.add("2");
        test.add("3");
        test.add("4");


        ArrayList<String> test1 = new ArrayList<>();
        test1.add("3");
        test1.add("4");
        test1.add("5");
        test1.add("6");
        test1.add("7");
        test1.add("8");

        test.retainAll(test1); //实现集合A

        test.removeAll(test1); //实现集合B

        test1.removeAll(test);
        test.addAll(test1);//实现A+B+C

        //实现A+C
        ArrayList<String> newTest = new ArrayList<>();
        newTest.addAll(test);
        newTest.addAll(test1);
        test.retainAll(test1);
        newTest.removeAll(test);
    }
}

猜你喜欢

转载自blog.csdn.net/c_he_n/article/details/80316839