1 package cn.itcast.p3.collection.demo;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 public class CollectionDemo {
7
8 public static void main(String[] args) {
9 // TODO Auto-generated method stub
10 Collection coll = new ArrayList();
11
12 show(coll);
13
14 Collection c1 = new ArrayList();
15 Collection c2 = new ArrayList();
16
17 show(c1,c2);
18 }
19
20 public static void show(Collection c1,Collection c2) {
21 //给c1添加元素。
22 c1.add("abc1");
23 c1.add("abc2");
24 c1.add("abc3");
25 c1.add("abc4");
26
27 //给c2添加元素。
28 c2.add("abc2");
29 c2.add("abc6");
30 c2.add("abc7");
31 System.out.println("c1:"+c1);
32 System.out.println("c2:"+c2);
33
34 //演示addAll
35 c1.addAll(c2);//c2中的元素添加到c1中。
36 System.out.println("c1:"+c1);
37
38 //演示removeAll //将两个集合中相同元素从调用removeAll的集合中删除
39 // bollean b = c1.remove(c2);
40 // System.out.println("removeAll:"+b);
41
42 //演示containsAll
43 // boolean b = c1.contains(c2);
44 // System.out.println("containsAll:"+b);
45
46 //演示retainAll //取交集,保留和指定的集合相同的元素,而删除不同的元素
47 //与removeAll功能相反
48 boolean b = c1.retainAll(c2);
49 System.out.println("retainAll:"+b);
50 System.out.println("c1:"+c1);
51
52 }
53
54 public static void show(Collection coll) {
55 //1,添加元素。add.
56 coll.add("abc1");
57 coll.add("abc2");
58 coll.add("abc3");
59 System.out.println(coll);
60 //2,删除元素。remove
61 coll.remove("abc2");//会改变集合的长度
62 //清空集合
63 coll.clear();
64 System.out.println(coll.contains("abc4"));
65 System.out.println(coll);
66 }
67
68 }