隨筆 - 303  文章 - 883  trackbacks - 0
          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(357)

          我管理的群

          公共blog

          • n維空間
          • Email : java3d@126.com 群 : 12999758

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網(wǎng)站

          搜索

          •  

          最新評(píng)論

          from:http://ydlinfo.com/

          在5.0中,collection最大的一個(gè)改變就是可以指定它的具體類型:
          List<String>?list=new?List<String>;

          兩個(gè)最基本的接口:
          public?interface?Collection<E>
          {
          boolean?add(E?element);
          Iterator<E>?iterator();
          .?.?.
          }

          public?interface?Iterator<E>
          {
          E?next();
          boolean?hasNext();
          void?remove();
          }

          在5.0以前,常用的形式就是:
          Collection<String>?c?=?.?.?.;
          Iterator<String>?iter?=?c.iterator();
          while?(iter.hasNext())
          {
          String?element?=?iter.next();
          do?something?with?element
          }
          但是在5.0中加入另外一種循環(huán)方式,類似于for?each:
          for?(String?element?:?c)
          {
          do?something?with?element
          }
          這種方式對(duì)任何實(shí)現(xiàn)了Iterable接口的類都適用。

          在使用remove的時(shí)候特別要注意的一點(diǎn)是,在調(diào)用remove之前必須先調(diào)用一次next方法,因?yàn)閚ext就像是在移動(dòng)一個(gè)指針,remove刪掉的就是指針剛剛跳過去的東西。即使是你想連續(xù)刪掉兩個(gè)相鄰的東西,也必須在每次刪除之前調(diào)用next。

          對(duì)collection排序和查找
          Collections類的sort方法可以對(duì)任何實(shí)現(xiàn)了List接口的類進(jìn)行排序。在排序過程中,他默認(rèn)這些類實(shí)現(xiàn)了Comparable接口,如果想用其他方法排序,可以在調(diào)用sort方法的時(shí)候提供一個(gè)Comparator對(duì)象:
          Comparator<Item>?itemComparator?=?new
          Comparator<Item>()
          {
          public?int?compare(Item?a,?Item?b)
          {
          return?a.partNumber?-?b.partNumber;
          }
          });
          反向排序:
          Collections.sort(items,?itemComparator);
          Collections.sort(items,?Collections.reverseOrder(itemComparator));

          查找一個(gè)對(duì)象:
          i?=?Collections.binarySearch(c,?element);
          i?=?Collections.binarySearch(c,?element,?comparator);
          但是這些list必須是已經(jīng)排好序了。而且要注意的是這個(gè)算法需要隨機(jī)訪問collection,如果不支持隨機(jī)訪問那么這個(gè)算法的效率可能會(huì)很低。

          幾種常用Collection:
          ArrayList
          An?indexed?sequence?that?grows?and?shrinks?dynamically
          可以隨機(jī)訪問,但是如果要從中間刪除一個(gè)對(duì)象會(huì)影響效率,因?yàn)橛行┪磩h除的對(duì)象要相應(yīng)的調(diào)整位置。非線程安全,但效率會(huì)比Vector要高,如果在單線程下,選它而不是Vector。

          LinkedList
          An?ordered?sequence?that?allows?efficient?insertions?and?removal?at?any?location
          只能按順序訪問,添加刪除很方便。雖然提供了get(n)方法,但實(shí)際上還是順序訪問的,如果發(fā)現(xiàn)在LinkedList里面使用了這個(gè)方法,要考慮這個(gè)List類型是否選的合適

          HashSet
          An?unordered?collection?that?rejects?duplicates
          以hashcode為索引,適用于不知道所存對(duì)象位置而想尋找某個(gè)對(duì)象的情況。不可重復(fù)

          TreeSet
          A?sorted?set
          與HashSet類似,但是所存對(duì)象是排了序的

          LinkedHashSet
          A?set?that?remembers?the?order?in?which?elements?were?inserted


          PriorityQueue
          A?collection?that?allows?efficient?removal?of?the?smallest?element
          加入Queue的時(shí)候會(huì)給與一個(gè)優(yōu)先級(jí),從queue中取出的時(shí)候先取出優(yōu)先級(jí)最低的

          HashMap
          A?data?structure?that?stores?key/value?associations
          存儲(chǔ)key/value對(duì),非線程安全,與HashTable相比效率要高些

          treeMap
          A?map?in?which?the?keys?are?sorted
          排序的HashMap

          LinkedHashMap
          A?map?that?remembers?the?order?in?which?entries?were?added?



          地震讓大伙知道:居安思危,才是生存之道。
          posted on 2007-03-26 23:37 小尋 閱讀(895) 評(píng)論(1)  編輯  收藏 所屬分類: j2se/j2ee/j2me

          FeedBack:
          # re: [轉(zhuǎn)]collection使用總結(jié) 2007-04-01 22:17 皿里
          不錯(cuò),幫頂!!!!!  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 五台县| 册亨县| 离岛区| 昌都县| 成都市| 湘潭市| 白河县| 精河县| 博爱县| 长岛县| 中牟县| 夹江县| 墨江| 子长县| 汶川县| 汕尾市| 枣庄市| 富阳市| 霸州市| 托克逊县| 镇赉县| 陵川县| 丹巴县| 乾安县| 札达县| 巩义市| 越西县| 巨鹿县| 田阳县| 读书| 仙游县| 左云县| 武汉市| 五家渠市| 沭阳县| 西峡县| 清新县| 佛冈县| 宜城市| 古田县| 来宾市|