<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    walterwing  
    日歷
    <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789
    統(tǒng)計
    • 隨筆 - 12
    • 文章 - 1
    • 評論 - 7
    • 引用 - 0

    導(dǎo)航

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

     
    前面介紹完了幾個Collections Framework的幾個主要接口,下面來講一下對象是如何排序的,為下面的SortedSet和SortedMap做準備。

    舉個例子,我們有個list,里面存儲了若干個String,那么我們可以用下面的方法來對list內(nèi)部的String按照字母順序來排序:

    Collections.sort(list);

    那如果list里存放的不是String,而是其他類型的對象呢?能否排序就取決于該對象是否實現(xiàn)了Comparable這個接口,如果沒有的話,Collections.sort()方法就將拋出ClassCastExcepton。

    下面的表格列出了java里實現(xiàn)了Comparable接口的一些重要的類:

    Classes Implementing Comparable
    Class Natural Ordering
    Byte Signed numerical
    Character Unsigned numerical
    Long Signed numerical
    Integer Signed numerical
    Short Signed numerical
    Double Signed numerical
    Float Signed numerical
    BigInteger Signed numerical
    BigDecimal Signed numerical
    Boolean Boolean.FALSE < Boolean.TRUE
    File System-dependent lexicographic on path name
    String Lexicographic
    Date Chronological
    CollationKey Locale-specific lexicographic

    如果你想對自己的類實現(xiàn)Comparable接口,一定要先參考API文檔關(guān)于Comparable接口的說明。

    另外,如果你想用自己的排序屏蔽原本的排序方式,又或者你想對某個類的對象排序,但該類并沒有實現(xiàn)Comparable接口,這個時候,你就可以用Comparator接口來實現(xiàn)你自己的排序方式。

    但有一點要注意,排序也不是亂排的,應(yīng)該盡量和類方法中的equals方法保持一致,尤其是當Comparator用在SortedSet或者SortedMap中的時候(這兩個可以通過指定Comparator來覆蓋自然排序方式)。

    舉個例子來說:

    public class Cmp {

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            SortedSet set 
    = new TreeSet();
            set.add(
    "a");
            set.add(
    "a");
            
            Iterator it 
    = set.iterator();
            
    while(it.hasNext()) {
                System.out.println(it.next());
            }
            
        }

    }

    我們知道Set是不允許有重復(fù)元素的,所以即使我們加入了兩個“a”,最后輸出的結(jié)果仍然是只有一個“a”。

    但如果我們對程序做如下修改,指定自己的Comparator:
    public class Cmp {
        
        
    static final Comparator cp = new Comparator() {
            
    public int compare(Object o1, Object o2) {
                
    return 1;
            }
        };

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            SortedSet set 
    = new TreeSet(cp);
            set.add(
    "a");
            set.add(
    "a");
            
            Iterator it 
    = set.iterator();
            
    while(it.hasNext()) {
                System.out.println(it.next());
            }
            
        }

    }

    這次輸出的結(jié)果將是兩個"a",這與Set本身的規(guī)范并不相符。



    6. SortedSet

    SortedSet一種按升序來排列元素的Set。排序的規(guī)則是按照元素的自然順序,或是按照構(gòu)造函數(shù)中指定的Comparator規(guī)定的順序。

    下面是SortedSet的定義:

    public interface SortedSet<E> extends Set<E> {
        
    // Range-view
        SortedSet<E> subSet(E fromElement, E toElement);
        SortedSet
    <E> headSet(E toElement);
        SortedSet
    <E> tailSet(E fromElement);

        
    // Endpoints
        E first();
        E last();

        
    // Comparator access
        Comparator<? super E> comparator();
    }

    我們看到,SortedSet提供了一些特殊的操作:

    • Range view — allows arbitrary range operations on the sorted set
    • Endpoints — returns the first or last element in the sorted set
    • Comparator access — returns the Comparator, if any, used to sort the set
    至于其他的從Set繼承過來的操作,基本都和Set一樣。之所以說“基本”,是因為有兩個例外:

    ·SortedSet得到的iterator是按順序進行遍歷的(Set沒有順序)
    ·toArray得到的數(shù)組是按序排列SortedSet中的元素的(Set沒有順序)

    SortedSet (Java 2 Platform SE 6)

    所有通用有序 set 實現(xiàn)類都應(yīng)該提供 4 個“標準”構(gòu)造方法:

    1) void(無參數(shù))構(gòu)造方法,它創(chuàng)建一個空的有序 set,按照元素的自然順序進行排序。

    2) 帶有一個 Comparator 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個空的有序 set,根據(jù)指定的比較器進行排序。

    3) 帶有一個 Collection 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個新的有序 set,其元素與參數(shù)相同,按照元素的自然順序進行排序。

    4) 帶有一個 SortedSet 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個新的有序 set,其元素和排序方法與輸入的有序 set 相同。如何做到這一點?別忘了SortedSet本身就有一個comparator()方法,可以返回他所用的Comparator。但要注意的是無法保證強制實施此建議,因為接口不能包含構(gòu)造方法。


    在介紹List的時候,我們曾經(jīng)強調(diào),List里的range-view方法List<E> subList(int from, int to);是要嚴格避免濫用的。因為在subList的作用域范圍內(nèi),任何對list本身的修改都將使subList的行為不可預(yù)期。

    但是在SortedSet里情況就不一樣了,你應(yīng)該注意到,SortedSet里的三個range-view方法,都是根據(jù)提供的比較范圍來截取subSet,而不是像List里那樣使用索引。這樣在subSet的作用域范圍內(nèi),對set本身的修改都將反映到subSet上,反之亦然。參見下面的例子:


    public class Cmp {

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
    //        SortedSet set = new TreeSet(cp);
            SortedSet set = new TreeSet();
            set.add(
    "a");
            set.add(
    "b");
            set.add(
    "c");
            set.add(
    "d");
            set.add(
    "e");
            
            SortedSet sub 
    = set.tailSet("c");
            
            Iterator it 
    = sub.iterator();
            
    while(it.hasNext()) {
                System.out.println(it.next());
            }
            
            set.add(
    "f");
            
            System.out.println(
    "-------------------");
            
            it 
    = sub.iterator();
            
    while(it.hasNext()) {
                System.out.println(it.next());
            }
            
        }

    }

    輸出的結(jié)果是:

    c
    d
    e
    -------------------
    c
    d
    e
    f

    可以看到,我們得到“值大于等于c”的subSet后,當我們再向原來的set加入一個新的元素“f”,由于這個元素也大于“c”,那么這個元素也將反映到subSet上。


    下面介紹一個比較特殊的小技巧:通過使用subSet,如何得到一個閉包的結(jié)果?
    我們知道,如果按照一般的調(diào)用,下面的代碼是獲得大于等于“doorbell”而小于“pickle”的subSet:

    dictionary.subSet("doorbell""pickle");

    而我們可以通過下面方法得到大于等于“doorbell”而小于等于“pickle”的subSet:

    dictionary.subSet("doorbell""pickle\0");

    同理,我們可以通過下面方法得到大于“doorbell”而小于“pickle”的subSet:

    dictionary.subSet("doorbell\0""pickle");


    7. SortedMap

    SortedMap是對key進行升序排列的Map。

    下面是其定義:

    public interface SortedMap<K, V> extends Map<K, V>{
        Comparator
    <? super K> comparator();
        SortedMap
    <K, V> subMap(K fromKey, K toKey);
        SortedMap
    <K, V> headMap(K toKey);
        SortedMap
    <K, V> tailMap(K fromKey);
        K firstKey();
        K lastKey();
    }

    和SortedSet類似,SortedMap也提供了如下一些額外的操作:
    • Range view — performs arbitrary range operations on the sorted map
    • Endpoints — returns the first or the last key in the sorted map
    • Comparator access — returns the Comparator, if any, used to sort the map
    ·SortedMap得到的iterator是按順序進行遍歷的(Map沒有順序)
    ·toArray得到的數(shù)組是按序排列SortedMap中的元素的(Map沒有順序)

    SortedMap (Java 2 Platform SE 6)

    注意,如果有序映射要正確實現(xiàn) Map 接口,則有序映射所維持的順序(無論是否提供了明確的比較器)都必須與 equals 一致。(有關(guān)與 equals 一致 的精確定義,請參閱 Comparable 接口或 Comparator 接口)。這是因為 Map 接口是按照 equals 操作定義的,但有序映射使用它的 compareTo(或 compare)方法對所有鍵進行比較,因此從有序映射的角度來看,此方法認為相等的兩個鍵就是相等的。即使順序與 equals 不一致,樹映射的行為仍然 定義良好的,只不過沒有遵守 Map 接口的常規(guī)協(xié)定。

    所有通用有序映射實現(xiàn)類都應(yīng)該提供 4 個“標準”構(gòu)造方法:1) void(無參數(shù))構(gòu)造方法,它創(chuàng)建一個空的有序映射,按照鍵的自然順序進行排序。2) 帶有一個 Comparator 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個空的有序映射,根據(jù)指定的比較器進行排序。3) 帶有一個 Map 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個新的有序映射,其鍵-值映射關(guān)系與參數(shù)相同,按照鍵的自然順序進行排序。4) 帶有一個 SortedMap 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個新的有序映射,其鍵-值映射關(guān)系和排序方法與輸入的有序映射相同。無法保證強制實施此建議,因為接口不能包含構(gòu)造方法。




    總結(jié)

    The Java Collections Framework hierarchy consists of two distinct interface trees:

    • The first tree starts with the Collection interface, which provides for the basic functionality used by all collections, such as add and remove methods. Its subinterfaces — Set, List, and Queue — provide for more specialized collections.

      The Set interface does not allow duplicate elements. This can be useful for storing collections such as a deck of cards or student records. The Set interface has a subinterface, SortedSet, that provides for ordering of elements in the set.

      The List interface provides for an ordered collection, for situations in which you need precise control over where each element is inserted. You can retrieve elements from a List by their exact position.

      The Queue interface enables additional insertion, extraction, and inspection operations. Elements in a Queue are typically ordered in on a FIFO basis.

    • The second tree starts with the Map interface, which maps keys and values similar to a Hashtable.

      Map's subinterface, SortedMap, maintains its key-value pairs in ascending order or in an order specified by a Comparator.




    posted on 2008-07-03 21:54 This is Wing 閱讀(358) 評論(0)  編輯  收藏 所屬分類: Java基礎(chǔ)
     
    Copyright © This is Wing Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 狠狠亚洲婷婷综合色香五月排名| 男人扒开添女人下部免费视频| 久久九九免费高清视频| 日韩亚洲国产二区| 人人爽人人爽人人片A免费| 亚洲欧洲自拍拍偷精品 美利坚| 亚洲男同gay片| 日本免费一区尤物| 久久久久亚洲国产AV麻豆| 在线观看永久免费视频网站| 朝桐光亚洲专区在线中文字幕| 亚洲第一页综合图片自拍| 中文字幕av免费专区| 少妇中文字幕乱码亚洲影视| 亚洲一区二区三区免费视频| 亚洲乱色伦图片区小说| 亚洲精品一级无码中文字幕| 国产一区二区三区免费| 亚洲一级免费毛片| 国产在线观看www鲁啊鲁免费| 国产亚洲综合精品一区二区三区| 国产精品亚洲综合专区片高清久久久| 免费看无码特级毛片| 亚洲日韩乱码中文无码蜜桃 | 精品亚洲aⅴ在线观看| 色片在线免费观看| 看亚洲a级一级毛片| 国产AV无码专区亚洲精品| 91频在线观看免费大全| 亚洲欧洲国产综合AV无码久久| 亚洲一区二区视频在线观看| 最近免费mv在线电影| 亚洲AV无码资源在线观看| 国产午夜亚洲精品午夜鲁丝片| 18禁成人网站免费观看| 激情小说亚洲图片| 97亚洲熟妇自偷自拍另类图片| 看全色黄大色大片免费久久| 国产午夜无码精品免费看动漫| 亚洲无人区码一二三码区别图片| 色噜噜AV亚洲色一区二区|