1. gzyueqian
      13352868059
      首頁 > 新聞中心 > > 正文

      基于JDK5.0一些collection類的使用總結

      更新時間: 2008-04-14 14:58:42來源: 粵嵌教育瀏覽量:766

        【賽迪網技術社區整理】

        在5.0中,collection的一個改變就是可以指定它的具體類型:

        List list=new List;

        兩個基本的接口:

        public interface Collection

        {

        boolean add(E element);

        Iterator iterator();
        . . .

        }

        public interface Iterator

        {

        E next();

        boolean hasNext();

        void remove();

        }

        在5.0以前,常用的形式就是:

        Collection c = . . .;

        Iterator iter = c.iterator();

        while (iter.hasNext())

        {

        String element = iter.next();

        do something with element

        }

        但是在5.0中加入另外一種循環方式,類似于for each:

        for (String element : c)

        {

        do something with element

        }

        這種方式對任何實現了Iterable接口的類都適用。

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

        對collection排序和查找

        Collections類的sort方法可以對任何實現了List接口的類進行排序。在排序過程中,他默認這些類實現了Comparable接口,如果想用其他方法排序,可以在調用sort方法的時候提供一個Comparator對象:

        Comparator itemComparator = new

        Comparator()

        {

        public int compare(Item a, Item b)

        {

        return a.partNumber - b.partNumber;

        }

        });

        反向排序:

        Collections.sort(items, itemComparator);

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

        查找一個對象:

        i = Collections.binarySearch(c, element);

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

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

        幾種常用Collection:

        ArrayList

        An indexed sequence that grows and shrinks dynamically

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

        LinkedList

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

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

        HashSet

        An unordered collection that rejects duplicates

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

        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的時候會給與一個優先級,從queue中取出的時候先取出優先級的

        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

      免費預約試聽課

      亚洲另类欧美综合久久图片区_亚洲中文字幕日产无码2020_欧美日本一区二区三区桃色视频_亚洲AⅤ天堂一区二区三区

      
      

      1. 亚洲午夜aⅴ视频 | 亚洲精品无播放器在线观看 | 亚洲天堂2021AV在线 | 一日本道a高清免费播放 | 亚洲三级视频专区 | 日本在线精品视频观看 |