交集

1
2
3
4
5
6
7
8
9
List<String> list1 = new ArrayList<String>();
list1.add("A");
list1.add("B");
List<String> list2 = new ArrayList<String>();
list2.add("B");
list2.add("C");

list1.retainAll(list2);
System.out.println("交集:" + list1);//交集:[B]

差集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
List<String> all = new ArrayList<>();
all.add("111");
all.add("222");
all.add("333");

List<String> s1 = new ArrayList<>();
s1.add("111");
s1.add("444");
s1.add("555");

all.removeAll(s1);

for (String s : all) {
System.out.println(s); //222 333
}

数组

1
2
3
String[] arr = new String[]{"hello", "world"};

Person[] pList = new Person[]{new Person("李四"), new Person("张三")};