大师网-带你快速走向大师之路 解决你在学习过程中的疑惑,带你快速进入大师之门。节省时间,提升效率

JAVA List 交集 并集 差集 去重复并集

简单类型

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class App {

    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add("1111");
        list1.add("2222");
        list1.add("3333");

        List<String> list2 = new ArrayList<String>();
        list2.add("3333");
        list2.add("4444");
        list2.add("5555");

        // 并集
        // list1.addAll(list2);
        // 交集
        // list1.retainAll(list2);
        // 差集
        // list1.removeAll(list2);
        // 无重复并集
        list2.removeAll(list1);
        list1.addAll(list2);

        Iterator<String> it = list1.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());

        }
    }
}

复杂类型

比如自定义的Person类,我猜测判断两个对象是否相同可能是通过调用对象的equals方法,所以如果对复杂类型操作需要重写equals方法

源码分析以removeAll为例:

public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        //调用batchRemove方法
        return batchRemove(c, false);
    }
private boolean batchRemove(Collection<?> c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            for (; r < size; r++)
                //调用contains方法
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            if (r != size) {
                System.arraycopy(elementData, r,
                                 elementData, w,
                                 size - r);
                w += size - r;
            }
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
            }
        }
        return modified;
    }
public boolean contains(Object o) {
        //调用indexOf方法
        return indexOf(o) >= 0;
    }
public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                //最后调用了equals方法
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

Demo:

public class Person {

    private String name;
    private int age;

    public Person() {

    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Person)){
            return false;
        }
        Person p = (Person) obj;
        if(this.name.equals(p.getName())&&this.age==p.getAge()){
            return true;
        }

        return false;
    }
}
import java.util.ArrayList;

public class App {

    public static void main(String[] args) {

        Person a = new Person("a", 1);
        Person b = new Person("b", 1);

        Person c = new Person("a", 1);
        Person d = new Person("b", 1);

        Person e = new Person("c", 1);
        Person f = new Person("d", 1);

        ArrayList<Person> list1 = new ArrayList<Person>();

        list1.add(a);
        list1.add(b);
        list1.add(e);

        ArrayList<Person> list2 = new ArrayList<Person>();

        list2.add(c);
        list2.add(d);
        list2.add(f);

        list1.removeAll(list2);
        list2.addAll(list1);

        for (Person person : list2) {
            System.out.println(person.getName());
        }

    }

}

最后输出为:

a
b
d
c