J2SDK所提供的容器API位于java.util包內,容器API的類圖結構如下:?
左邊是一個一個裝,右邊是一對一對來裝?Set的數據對象沒有順序并且不可以重復,List有順序可以重復(互相的equals)?Map接口定義了存儲“鍵(key)—值(value)映射對”的方法?2.Collection接口?Collection接口中所定義的方法:?int size();
boolean isEmpty();?void clear();
boolean contains(Object element);? 是不是包含某個對象boolean add(Object element);?boolean remove(Object element);? 去除Iterator iterator();?boolean containsALL(Collection c);? 是不是包含另一個集合中的所有元素boolean addALL(Collection c);??boolean removeALL(Collection c);??boolean retainALL(Collection c);?? 求交集Object[] toArray();?例如:?import java.util.*;
public class Name { PRivate String s; private String c; public Name(String s, String c) { this.s = s; this.c = c; } public String toString() { return s + " " + c; }}public class Cllection { public static void main(String agrs[]) { Collection c = new ArrayList(); //使用父類引用指向子類對象,可以放入不同類型的對象 c.add("hello"); c.add(new Integer(100)); //只能添加對象,不能添加基礎的數據類型 c.add(new Name("f1", "l1")); System.out.println(c.size()); System.out.println(c); }}結果是:3 [hello, 100, f1 l1]?3.Collection方法舉例?容器類對象在調用remov、contains等方法時需要比較對象是否相等,這回涉及到對象類型的equals方法和hashCode?方法;對于自定義的類型,需要重寫equals方法以實現自定義的對象相等規則。?注意:相等的對象應該具有相等的hash Codes。?重寫equals方法時同時也要重寫hashCode。增加Name類的equals和hashCode方法如下:??public class Name { private String s; private String c; public Name(String s, String c) { this.s = s; this.c = c; } public String getS() { return s; } public String getC() { return c; } public String toString() { return s + " " + c; } public boolean equals(Object obj) { if (obj instanceof Name) { Name name = (Name) obj; return (s.equals(name.s)) && (s.equals(name.s)); } return super.equals(obj); } public int hashCode() { //地址,索引 return s.hashCode(); }}public class Cllection { public static void main(String agrs[]) { Collection c = new HashSet(); //使用父類引用指向子類對象,可以放入不同類型的對象 c.add("hello"); c.add(new Integer(100)); //只能添加對象,不能添加基礎的數據類型 c.add(new Name("f1", "l1")); c.remove("hello"); c.remove(new Integer(100)); System.out.println(c.remove(new Name("f1", "l1"))); //equals時才去除,返回true,這兩個不是同一個對象 System.out.println(c); }}結果是:true []?如果沒有重寫equals方法時,結果是:?false [f1 l1]4.Iterator接口?所有實現了Collection接口的容器類都有一個iterator方法用以返回一個實現了Iterator接口的對象。Iterator是?統一的來遍歷Collection里所有元素的方法Iterator對象稱作迭代(重復反饋過程的活動)器,用以方便的實現對容器內元素的遍歷操作。?Iterator接口定義了如下方法:?boolean hasNext(); //判斷游標右邊是否有元素?Object next(); //返回游標右邊的元素并將游標移動到下一個位置?void remove(); //刪除游標左邊的元素,在執行完next之后該操作只能執行一次?public class TestIterator { public static void main(String agrs[]) { Collection c = new HashSet(); c.add(new Name("f1", "l1")); c.add(new Name("f2", "l2")); c.add(new Name("f3", "l3")); Iterator i = c.iterator(); while (i.hasNext()) { Name n = (Name) i.next(); System.out.println(n.getS() + " "); } }}結果是:f1 f2 f3Iterator對象的remove方法是在迭代過程中刪除元素的唯一的安全方法。例:public class TestIterator { public static void main(String agrs[]) { Collection c = new HashSet(); c.add(new Name("f1", "1111")); c.add(new Name("f2", "l2")); c.add(new Name("f3", "1113")); for (Iterator i = c.iterator(); i.hasNext(); ) { Name n = (Name) i.next(); if (n.getC().length() < 3) { i.remove(); } } System.out.println(c); }}結果是:[f1 1111, f3 1113]?5.JDK1.5增強的for循環?增強的for循環對于遍歷array或collection的時候相當簡便?缺陷:數組:不能方便的訪問下標值?集合:與使用Iterator相比,不能方便的刪除集合中的內容,在內部也是調用Iterator?出了簡單遍歷并讀出其中的內容外,不建議使用增強for?例:public class TestEnhanceFor { public static void main(String agrs[]) { Collection c = new HashSet(); c.add(new Name("a", "aa")); c.add(new Name("b", "bb")); c.add(new Name("c", "cc")); for (Object o : c) { System.out.println(o); } }}結果:a aab bb
c cc6.Set接口?Set接口是Collection的子接口,Set接口沒有提供額外的方法,但實現Set接口的容器類中的元素是沒有順序的,而且不可以重復。?Set容器可以與數學中“集合”的概念相對應,有HashSet,TreeSet等??
public class TestSet { public static void main(String ages[]) { Set s = new HashSet(); s.add("hello"); s.add("world"); s.add(new Name("f1", "l1")); s.add(new Integer(100)); s.add(new Name("f1", "l1"));//如果Name中沒有重寫equals方法,該項就會被添加,如果重寫了,就不會添加 s.add("hello"); //相同元素不會被加入 System.out.println(s); }}結果是:[world, 100, hello, f1 l1]?
public class TestSet { public static void main(String ages[]) { Set s1 = new HashSet(); Set s2 = new HashSet(); s1.add("a"); s1.add("b"); s1.add("c"); s2.add("a"); s2.add("d"); s2.add("b");//Set和List容器類都具有 //Collection(Collection c)構造方法用于初始化容器類 Set sn = new HashSet(s1); sn.retainAll(s2); //交集 Set su = new HashSet(s1); su.addAll(s2); System.out.println(sn); System.out.println(su); }}結果是:[a, b]
[a, b, c, d]
新聞熱點
疑難解答