1 package com.family.ch07;
2
3 import java.sql.Array;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.Date;
8 import java.util.Iterator;
9
10 import org.junit.Test;
14 public class TestCollection {
15 @Test
16 public void testCollection1(){
17 Collection coll=new ArrayList();
18 //1、size();返回集合中元素的个数
19 System.out.println(coll.size());
20 //2、add(Object Object);向集合中添加一个元素
21 coll.add(123);
22 coll.add("AA");
23 coll.add(new Date());
24 coll.add("BB");
25 System.out.println(coll.size());
26 //3、addAll(Collection collection);将形参collection中包含的所有元素添加到当前集合中
27 Collection coll1=Arrays.asList(1,2,3);//把数组添加到集合当中
28 coll.addAll(coll1);
29 System.out.println(coll.size());
30 //查看集合元素
31 System.out.println(coll);
32 //4、isEmpty():判断集合是否为空
33 System.out.println(coll.isEmpty());//一开始coll集合中添加了元素,所以值为false
34 //5、clear():清空集合元素
35 coll.clear();
36
37 System.out.println();
38 }
39 @Test
40 public void testCollection2(){
41 Collection coll=new ArrayList();
42 coll.add(123);
43 coll.add("AA");
44 coll.add(new Date());
45 coll.add("BB");
46 coll.add(new Person("MM", 23));//①
47 System.out.println(coll);
48 //6、contains(Object Object):判断集合中是否包含指定的Object元素。如果包含,返回true,反之返回false
49 //判断的依据:根据元素所在的类的equals()方法进行判断
50 //明确:如果存入集合中的元素是自定义的对象。要求:自定义类要重写equals()方法!
51 boolean b1=coll.contains("678");//判断集合中是否有678,没有所以返回false。
52 System.out.println(b1);
53 boolean b2=coll.contains("AA");//判断集合中是否有AA,有所以返回true。
54 System.out.println(b2);
55 boolean b3=coll.contains(new Person("MM", 23));
56 System.out.println(b3);//①要想输出为true,必须在Person类重写地址,否则输出为false
57 //7、containsAll(Collection collection):判断当前集合中是否包含collection中所有的元素
58 Collection coll1=new ArrayList();
59 coll1.add(123);
60 coll1.add("AA");
61 boolean b4=coll.containsAll(coll1);//判断coll里面是不是包含coll1的所有元素
62 System.out.println("#"+b4);
63 coll1.add(456);
64 //8、retainAll(Collection collection):求当前集合与collection的共有的元素,返回给当前集合
65 coll.retainAll(coll1);//找出coll集合与coll1集合中的共有的元素,然后返回给coll,输出共有元素
66 System.out.println(coll);
67 //9、remove(Object Object):删除集合中的Object元素。若删除成功,返回true,否则返回false
68 boolean b5= coll.remove("BB");
69 System.out.println(b5);
70 //10、removeAll(Collection collection):从当前集合中删除包含在collection中的元素
71 coll.removeAll(coll1);//从coll中删除包含在coll1中的元素
72 System.out.println(coll);
73 //11、equals(Object Object):判断集合中的所有元素是否完全相同
74 Collection coll2=new ArrayList();
75 coll2.add(123);
76 coll2.add("AA");
77 System.out.println(coll1.equals(coll2));
78
79 //*12、hashCode()
80 System.out.println(coll.hashCode());
81 //13、toArray():将集合转化为数组
82 Object[]obj=coll.toArray();
83 for (int i = 0; i < obj.length; i++) {
84 System.out.println(obj[i]);
85 }
86 //14、iterator():返回一个Iterator接口实现类的对象,进而实现集合的遍历
87 Iterator iterator=coll.iterator();
88 //方式一:不用
89 System.out.println(iterator.next());
90 System.out.println(iterator.next());
91 System.out.println(iterator.next());
92 //方式二:不用
93 for (int i = 0; i < coll.size(); i++) {
94 System.out.println(iterator.next());
95 }
96 //方式三:使用
97 while (iterator.hasNext()) {
98 System.out.println(iterator.next());
99
100 }
101 }
102 }
1 package com.family.ch07;
2
3 public class Person {
4 private String name;
5 private int age;
6
7 public Person() {
8 super();
9 }
10
11 public Person(String name, int age) {
12 super();
13 this.name = name;
14 this.age = age;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32
33 @Override
34 public String toString() {
35 return "Person [name=" + name + ", age=" + age + "]";
36 }
37
38 @Override
39 public boolean equals(Object obj) {
40 if (this == obj)
41 return true;
42 if (obj == null)
43 return false;
44 if (getClass() != obj.getClass())
45 return false;
46 Person other = (Person) obj;
47 if (age != other.age)
48 return false;
49 if (name == null) {
50 if (other.name != null)
51 return false;
52 } else if (!name.equals(other.name))
53 return false;
54 return true;
55 }
56
57
58 }