欢迎访问CSDN博客专栏CSDN专栏 Java全栈之路,Github主页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

public class TestCollection {

public static void main(String[] args) {

//获取Collection集合的引用
//Collection c1 = new Collection(); error 接口不能构造对象
//接口类型的引用指向了实现类的对象,形成了多态
Collection c1 = new ArrayList();

//向集合c1中增加元素
boolean b1 = c1.add(new Integer(1));
System.out.println("b1 = " + b1); //true
//自动调用toString(),编译器自动添加
//在编译阶段调用父类的方法,在运行阶段调用子类重写以后的方法,ArrayList类
//toString()方法默认打印的格式为:[元素1, 元素2, ...]
System.out.println("c1 = " + c1); //[1]

b1 = c1.add(new String("two"));
System.out.println("b1 = " + b1); //true
System.out.println("c1 = " + c1); //[1, two]

b1 = c1.add(new Student(1001, "zhangfei", 30));
System.out.println("b1 = " + b1); //true
//当打印集合中的元素时,本质上调用每个元素对应的toString()来打印
//[1, two, Student[id=1001, name=zhangfei, age=30]]
System.out.println("c1 = " + c1);

System.out.println("--------------------------------------");
//准备另外一个集合
Collection c2 = new ArrayList();
c2.add(3); //采用了自动装箱技术 int => Integer
System.out.println("c2 = " + c2); //[3]
c2.add("four");
System.out.println("c2 = " + c2); //[3, four]
System.out.println("当前集合的元素个数是:" + c2.size()); //2
System.out.println("当前集合的元素个数是:" + c1.size()); //3

System.out.println("--------------------------------------");
//将集合c2中的所有元素添加到集合c1中,本质上就是将c2的元素一个个取出来放入c1
b1 = c1.addAll(c2);
//本质上将c2看做一个元素,然后整体放入c1中
//b1 = c1.add(c2);
System.out.println("b1 = " + b1); //b1 = true
//[1, two, Student[id=1001, name=zhangfei, age=30], 3, four]
//[1, two, Student[id=1001, name=zhangfei, age=30], [3, four]]
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2); //[3, four]

System.out.println("--------------------------------------");
//判断集合c1中是否包含参数指定的单个元素
//[1, two, Student[id=1001, name=zhangfei, age=30], 3, four]
b1 = c1.contains(new Integer(1));
System.out.println("b1 = " + b1); //b1 = true
b1 = c1.contains(new Integer(2));
System.out.println("b1 = " + b1); //b1 = false
b1 = c1.contains(new String("two"));
System.out.println("b1 = " + b1); //b1 = true
//contains方法的原理:参数对象调用equals()方法与集合中的元素一个一个比较
//当Student类没有重写equals()方法时,则比较对象的地址,因此结果为false
//当Student类重写equals()方法后,则比较对象的内容,因此结果为true
b1 = c1.contains(new Student(1001, "zhangfei", 30));
System.out.println("b1 = " + b1); //false b1 = true

System.out.println("--------------------------------------");
//判断集合c1中是否包含参数指定的多个元素
//[1, two, Student[id=1001, name=zhangfei, age=30], 3, four]
//将集合c2中的元素一个个拿出来在c1中进行查找
//[3, four]
//b1 = c1.containsAll(c2);
//表示判断集合c1中是否有元素c2这个整体
b1 = c1.contains(c2);
System.out.println("b1 = " + b1); //b1 = true false

System.out.println("--------------------------------------");
//实现集合c1中单个元素的删除
b1 = c1.remove(new String("2"));
System.out.println("b1 = " + b1); //b1 = false
//[1, two, Student[id=1001, name=zhangfei, age=30], 3, four]
System.out.println("c1 = " + c1); //

b1 = c1.remove(new String("two"));
System.out.println("b1 = " + b1); //b1 = true
//[1, Student[id=1001, name=zhangfei, age=30], 3, four]
System.out.println("c1 = " + c1); //

System.out.println("--------------------------------------");
//[1, Student[id=1001, name=zhangfei, age=30], 3, four]
System.out.println("c1 = " + c1); //
//[3, four]
System.out.println("c2 = " + c2);

//实现集合c1中多个元素的删除
//表示从集合c1中将集合c2中的元素一个一个删除
b1 = c1.removeAll(c2);
//表示从集合c1中将集合c2这个元素整体删除
//b1 = c1.remove(c2);
System.out.println("b1 = " + b1); //true false
//[1, Student[id=1001, name=zhangfei, age=30], 3, four]
System.out.println("c1 = " + c1); //[1, Student[id=1001, name=zhangfei, age=30]]
System.out.println("c2 = " + c2); //[3, four]

System.out.println("--------------------------------------");
//实现集合c1中所有元素的删除
//c1.clear();
//System.out.println("c1 = " + c1); //[啥也没有]
//System.out.println(c1.isEmpty()); //true
//System.out.println(c1.size()); //0

System.out.println("--------------------------------------");
//计算两个集合的交集并保留到当前集合中
System.out.println("c1 = " + c1); //[1, Student[id=1001, name=zhangfei, age=30]]
System.out.println("c2 = " + c2); //[3, four]

//计算c1和c2的交集并保留到c1中,若c1集合中的内容发生了改变则返回true
b1 = c1.retainAll(c2);
System.out.println("b1 = " + b1); //true
System.out.println("c1 = " + c1); //[啥也没有]
System.out.println("c2 = " + c2); //[3, four]

System.out.println("--------------------------------------");
//计算两个集合的交集并保留到当前集合中
//计算c2和c2的交集并保留到c2中,c2中的内容不发生改变,因此返回false
b1 = c2.retainAll(c2);
System.out.println("b1 = " + b1); //false
System.out.println("c2 = " + c2); //[3, four]
}

}