J2SDK所提供的容器API位于java.util包內(nèi),容器API的類圖結(jié)構(gòu)如下:?

左邊是一個(gè)一個(gè)裝,右邊是一對(duì)一對(duì)來裝?Set的數(shù)據(jù)對(duì)象沒有順序并且不可以重復(fù),List有順序可以重復(fù)(互相的equals)?Map接口定義了存儲(chǔ)“鍵(key)—值(value)映射對(duì)”的方法?2.Collection接口?Collection接口中所定義的方法:?int size();
boolean isEmpty();?void clear();
boolean contains(Object element);? 是不是包含某個(gè)對(duì)象boolean add(Object element);?boolean remove(Object element);? 去除Iterator iterator();?boolean containsALL(Collection c);? 是不是包含另一個(gè)集合中的所有元素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(); //使用父類引用指向子類對(duì)象,可以放入不同類型的對(duì)象 c.add("hello"); c.add(new Integer(100)); //只能添加對(duì)象,不能添加基礎(chǔ)的數(shù)據(jù)類型 c.add(new Name("f1", "l1")); System.out.println(c.size()); System.out.println(c); }}結(jié)果是:3 [hello, 100, f1 l1]?3.Collection方法舉例?容器類對(duì)象在調(diào)用remov、contains等方法時(shí)需要比較對(duì)象是否相等,這回涉及到對(duì)象類型的equals方法和hashCode?方法;對(duì)于自定義的類型,需要重寫equals方法以實(shí)現(xiàn)自定義的對(duì)象相等規(guī)則。?注意:相等的對(duì)象應(yīng)該具有相等的hash Codes。?重寫equals方法時(shí)同時(shí)也要重寫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(); //使用父類引用指向子類對(duì)象,可以放入不同類型的對(duì)象 c.add("hello"); c.add(new Integer(100)); //只能添加對(duì)象,不能添加基礎(chǔ)的數(shù)據(jù)類型 c.add(new Name("f1", "l1")); c.remove("hello"); c.remove(new Integer(100)); System.out.println(c.remove(new Name("f1", "l1"))); //equals時(shí)才去除,返回true,這兩個(gè)不是同一個(gè)對(duì)象 System.out.println(c); }}結(jié)果是:true []?如果沒有重寫equals方法時(shí),結(jié)果是:?false [f1 l1]4.Iterator接口?所有實(shí)現(xiàn)了Collection接口的容器類都有一個(gè)iterator方法用以返回一個(gè)實(shí)現(xiàn)了Iterator接口的對(duì)象。Iterator是?統(tǒng)一的來遍歷Collection里所有元素的方法Iterator對(duì)象稱作迭代(重復(fù)反饋過程的活動(dòng))器,用以方便的實(shí)現(xiàn)對(duì)容器內(nèi)元素的遍歷操作。?Iterator接口定義了如下方法:?boolean hasNext(); //判斷游標(biāo)右邊是否有元素?Object next(); //返回游標(biāo)右邊的元素并將游標(biāo)移動(dòng)到下一個(gè)位置?void remove(); //刪除游標(biāo)左邊的元素,在執(zhí)行完next之后該操作只能執(zhí)行一次?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() + " "); } }}結(jié)果是:f1 f2 f3Iterator對(duì)象的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); }}結(jié)果是:[f1 1111, f3 1113]?5.JDK1.5增強(qiáng)的for循環(huán)?增強(qiáng)的for循環(huán)對(duì)于遍歷array或collection的時(shí)候相當(dāng)簡(jiǎn)便?缺陷:數(shù)組:不能方便的訪問下標(biāo)值?集合:與使用Iterator相比,不能方便的刪除集合中的內(nèi)容,在內(nèi)部也是調(diào)用Iterator?出了簡(jiǎn)單遍歷并讀出其中的內(nèi)容外,不建議使用增強(qiáng)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); } }}結(jié)果:a aa b bb
c cc6.Set接口?Set接口是Collection的子接口,Set接口沒有提供額外的方法,但實(shí)現(xiàn)Set接口的容器類中的元素是沒有順序的,而且不可以重復(fù)。?Set容器可以與數(shù)學(xué)中“集合”的概念相對(duì)應(yīng),有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方法,該項(xiàng)就會(huì)被添加,如果重寫了,就不會(huì)添加 s.add("hello"); //相同元素不會(huì)被加入 System.out.println(s); }}結(jié)果是:[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)構(gòu)造方法用于初始化容器類 Set sn = new HashSet(s1); sn.retainAll(s2); //交集 Set su = new HashSet(s1); su.addAll(s2); System.out.println(sn); System.out.println(su); }}結(jié)果是:[a, b]
[a, b, c, d]
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注