隨筆-144  評(píng)論-80  文章-1  trackbacks-0
          java.util

          Interface

          Collection

          RetainAll :保留兩個(gè)Collection的交集。注意,如果該Collection是由Arrays.asList轉(zhuǎn)換而來,那么這個(gè)方法會(huì)失敗。因?yàn)檗D(zhuǎn)換來的List接口不支持這個(gè)方法

          Samples

          public static void collectionTest()

              {

                  Collection c1 = new ArrayList();

                  Collection c2 = new ArrayList();

                  c1.add("aaa");

                  c1.add("bbb");

                  c1.add("ccc");

                  c2.add("ddd");

                  c2.add("ccc");

                  c2.add("eee");

                  boolean isRetainSucceed = false;

                  isRetainSucceed = c2.retainAll(c1);

                  System.out.println("isRetainSucceed = " + isRetainSucceed);

                  System.out.println("********** print collection c2 values ");

                  for (Iterator iter = c2.iterator(); iter.hasNext();)

                  {

                      String s = (String) iter.next();

                      System.out.println("s = " + s);

                  }

          }

          Enumeration

          太簡(jiǎn)單,參考文檔

          Comparator

          未使用過

          EventListener

          空接口

          Iterator

          Enumeration 的不同點(diǎn):

          1. 允許遍歷Collection時(shí)刪除對(duì)象

          2. 方法名字可讀性更好

          List

          實(shí)現(xiàn)的四個(gè)類:AbstractList, ArrayList, LinkedList, Vector

          List 特點(diǎn):

          1. 允許重復(fù)元素,允許null元素

          2. 推薦用Iterator遍歷,而不是用索引

          addAll : 加入Collection

          containsAll :是否包含Collection

          retainAll : 保留和Collection的交集

          subList : 返回指定索引區(qū)間的子List

          ListIterator

          1. 提供元素的雙向遍歷,而不是單向

          2. 遍歷時(shí)可改變存儲(chǔ)的元素

          3. 可動(dòng)態(tài)插入元素,插入的元素在當(dāng)前操作元素的上一個(gè)位置

          Samples

          public static void ListTest(){

                  System.out.println("**********ListTest begin:");

                  List list = new ArrayList();

                  list.add("aaa");

                  list.add("bbb");

                  list.add("ccc");

                  ListIterator iter = list.listIterator();

                  System.out.println("**************** Iterating List forward :");

                  while(iter.hasNext()){

                      String s = (String)iter.next();

                      System.out.println("**********element = " + s);

                  }

                  System.out.println("**************** Iterating List backward :");

                  while(iter.hasPrevious()){

                      String s = (String)iter.previous();

                      System.out.println("**********element = " + s);

                  }

                  System.out.println("**************** Add element into List :");

                  while(iter.hasNext()){

                      int i = iter.nextIndex();

                      if (i==2)

                          iter.add("ddd");

                      String s = (String)iter.next();

                      System.out.println("**********element = " + s);

                  }

                  System.out.println("**************** Iterating List backward after add element:");

                  while(iter.hasPrevious()){

                      String s = (String)iter.previous();

                      System.out.println("**********element = " + s);

                  }

                  System.out.println("**********ListTest end:");

              }

          Map

          KeySet :

          返回Set對(duì)象,然后可以遍歷這個(gè)Set。其中的每個(gè)元素都是Map.Entry對(duì)象

          Map.Entry.setValue :

          在遍歷Entry對(duì)象時(shí),可以改變?cè)?/SPAN>Key對(duì)應(yīng)的Value

          Samples

          public static void mapEntrySetTest(){

                  System.out.println("**********mapEntrySetTest begin:");

                  Map map = new HashMap();

                  map.put("first","aaa");

                  map.put("second","bbb");

                  map.put("third","ccc");

                  map.put("fourth","ddd");

                  Set set = map.entrySet();

                  Map.Entry entry = null;

                  System.out.println("********** print values in map :");

                  for(Iterator iter = set.iterator();iter.hasNext();){

                      entry = (Map.Entry)iter.next();

                      System.out.println("Key is :" + entry.getKey() + " and Value is :" + entry.getValue());

                      entry.setValue((String)entry.getValue() + "_setValueTest");

                  }

                  System.out.println("********** After set value ,iterating values in map :");

                  for(Iterator iter = set.iterator();iter.hasNext();){

                      entry = (Map.Entry)iter.next();

                      System.out.println("Key is :" + entry.getKey() + " and Value is :" + entry.getValue());           

                  }

                  System.out.println("**********mapEntrySetTest end:");

          }

          Observable and Observer

          暫未想通

          RandomAccess

          空接口。實(shí)現(xiàn)這個(gè)接口的List實(shí)現(xiàn)品,表示他們支持高速的隨機(jī)訪問元素。如果實(shí)現(xiàn)這個(gè)接口,理論上

          for (int i=0, n=list.size(); i < n; i++)

                   list.get(i);

          比下面代碼要快:

          for (Iterator i=list.iterator(); i.hasNext(); )

                   i.next();

          Set

          沒什么特別的,和Collection差不多


          轉(zhuǎn)自:http://blog.csdn.net/WalkingWithJava/

          posted on 2005-04-19 11:25 小力力力 閱讀(322) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 墨竹工卡县| 张家界市| 杂多县| 郎溪县| 邢台市| 郓城县| 冕宁县| 昭觉县| 万源市| 罗田县| 惠安县| 密山市| 浑源县| 民丰县| 乌鲁木齐市| 中阳县| 白城市| 织金县| 理塘县| 玉田县| 若尔盖县| 承德县| 安岳县| 多伦县| 松溪县| 凤庆县| 乌恰县| 平乡县| 西青区| 界首市| 福建省| 宁海县| 丹阳市| 嵊泗县| 青田县| 瓦房店市| 塘沽区| 甘谷县| 宝丰县| 如东县| 纳雍县|