retain /rɪˈteɪn/ 保留
list1.retainAll(list2)方法解释
Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection
list1仅仅保留list2中有的元素,没有的统统移出,注意这里修改了原集合list1
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的交集");
boolean b = list1.retainAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
list1 = [a, b, c, c]
list2 = [c, d, e]
两个集合的交集
b = true
list1 = [c, c]
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
// list1.add("c");
// list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的交集");
boolean b = list1.retainAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
list1 = [a, b]
list2 = [c, d, e]
两个集合的交集
b = true
list1 = []
List<String> list1 = new ArrayList<>();
// list1.add("a");
// list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的交集");
boolean b = list1.retainAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
list1 = [c, c]
list2 = [c, d, e]
两个集合的交集
b = false
list1 = [c, c]
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it’s nonempty.)
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的并集");
boolean b = list1.addAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
list1 = [a, b, c, c]
list2 = [c, d, e]
两个集合的并集
b = true
list1 = [a, b, c, c, c, d, e]
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
// list2.add("c");
// list2.add("d");
// list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的并集");
boolean b = list1.addAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
list1 = [a, b, c, c]
list2 = []
两个集合的并集
b = false
list1 = [a, b, c, c]
Removes from this list all of its elements that are contained in the specified collection (optional operation).
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的差集");
boolean b = list1.removeAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
list1 = [a, b, c, c]
list2 = [c, d, e]
两个集合的差集
b = true
list1 = [a, b]
list1.xxxAll(list2)