日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JDK5.0中一些collection類的使用詳解

在JDK5.0中,collection最大的一個改變就是可以指定它的具體類型,具體有哪些呢,讓我們來看看:

創(chuàng)新互聯(lián)主營樊城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,樊城h5小程序制作搭建,樊城網(wǎng)站營銷推廣歡迎樊城等地區(qū)企業(yè)咨詢

List list=new List;

JDK5.0中collection兩個最基本的接口:

 
 
 
  1. public interface Collection  {     
  2. boolean add(E element);    
  3.  Iterator iterator();    
  4.  . . .     
  5. }     
  6. public interface Iterator   {     
  7.   E next();    
  8.  boolean hasNext();    
  9.  void remove();   } 

在JDK5.0以前,常用的形式就是:

 
 
 
  1. Collection c = . . .;    
  2.  Iterator iter = c.iterator();     
  3. while (iter.hasNext())   {     
  4. String element = iter.next();    
  5.  do something with element    
  6.  }  
  7.  
  8. 但是在5.0中加入另外一種循環(huán)方式,類似于for each:  
  9.  
  10. for (String element : c)   {   
  11.  do something with element    
  12.  } 

這種方式對任何實現(xiàn)了Iterable接口的類都適用。

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

對collection排序和查找

JDK5.0中Collections類的sort方法可以對任何實現(xiàn)了List接口的類進(jìn)行排序。在排序過程中,他默認(rèn)這些類實現(xiàn)了Comparable接口,如果想用其他方法排序,可以在調(diào)用sort方法的時候提供一個Comparator對象:

 
 
 
  1. Comparator itemComparator = new   Comparator()   {   
  2.   public int compare(Item a, Item b)   {    
  3.   return a.partNumber - b.partNumber;    
  4.    }    
  5.  }; 

反向排序:

Collections.sort(items, itemComparator);

Collections.sort(items, Collections.reverseOrder(itemComparator));

查找一個對象:

i = Collections.binarySearch(c, element);  

i = Collections.binarySearch(c, element, comparator);

但是這些list必須是已經(jīng)排好序了。而且要注意的是這個算法需要隨機(jī)訪問collection,如果不支持隨機(jī)訪問那么這個算法的效率可能會很低。

JDK5.0中幾種常用Collection:

ArrayList

An indexed sequence that grows and shrinks dynamically

可以隨機(jī)訪問,但是如果要從中間刪除一個對象會影響效率,因為有些未刪除的對象要相應(yīng)的調(diào)整位置。非線程安全,但效率會比Vector要高,如果在單線程下,選它而不是Vector。

LinkedList

An ordered sequence that allows efficient insertions and removal at any location

只能按順序訪問,添加刪除很方便。雖然提供了get(n)方法,但實際上還是順序訪問的,如果發(fā)現(xiàn)在LinkedList里面使用了這個方法,要考慮這個List類型是否選的合適

HashSet

An unordered collection that rejects duplicates

以hashcode為索引,適用于不知道所存對象位置而想尋找某個對象的情況。不可重復(fù)

TreeSet

A sorted set

與HashSet類似,但是所存對象是排了序的

LinkedHashSet

A set that remembers the order in which elements were inserted

PriorityQueue

A collection that allows efficient removal of the smallest element

加入Queue的時候會給與一個優(yōu)先級,從queue中取出的時候先取出優(yōu)先級最低的

HashMap

A data structure that stores key/value associations

存儲key/value對,非線程安全,與HashTable相比效率要高些

treeMap

A map in which the keys are sorted

排序的HashMap

LinkedHashMap

A map that remembers the order in which entries were added

那么JDK5.0中一些collection類的常見使用就是這些了,是不是對你了解JDK5.0中的collection類有幫助呢?

【編輯推薦】

  1. JDK的概念、組成及JDK常用包
  2. JDK1.4在Windows下的環(huán)境配置
  3. JDK1.6在LINUX下的安裝配置
  4. JDK日志框架之實例結(jié)合STAF淺析
  5. JDK日志分級作為核心API最佳實踐淺析

本文標(biāo)題:JDK5.0中一些collection類的使用詳解
網(wǎng)站地址:http://www.5511xx.com/article/cdogjcp.html