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 }