<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)計(jì)
    • 隨筆 - 12
    • 文章 - 1
    • 評論 - 7
    • 引用 - 0

    導(dǎo)航

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

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

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

    Collections.sort(list);

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

    下面的表格列出了java里實(shí)現(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

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

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

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

    舉個(gè)例子來說:

    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ù)元素的,所以即使我們加入了兩個(gè)“a”,最后輸出的結(jié)果仍然是只有一個(gè)“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é)果將是兩個(gè)"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一樣。之所以說“基本”,是因?yàn)橛袃蓚€(gè)例外:

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

    SortedSet (Java 2 Platform SE 6)

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

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

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

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

    4) 帶有一個(gè) SortedSet 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個(gè)新的有序 set,其元素和排序方法與輸入的有序 set 相同。如何做到這一點(diǎn)?別忘了SortedSet本身就有一個(gè)comparator()方法,可以返回他所用的Comparator。但要注意的是無法保證強(qiáng)制實(shí)施此建議,因?yàn)榻涌诓荒馨瑯?gòu)造方法。


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

    但是在SortedSet里情況就不一樣了,你應(yīng)該注意到,SortedSet里的三個(gè)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后,當(dāng)我們再向原來的set加入一個(gè)新的元素“f”,由于這個(gè)元素也大于“c”,那么這個(gè)元素也將反映到subSet上。


    下面介紹一個(gè)比較特殊的小技巧:通過使用subSet,如何得到一個(gè)閉包的結(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進(jìn)行升序排列的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是按順序進(jìn)行遍歷的(Map沒有順序)
    ·toArray得到的數(shù)組是按序排列SortedMap中的元素的(Map沒有順序)

    SortedMap (Java 2 Platform SE 6)

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

    所有通用有序映射實(shí)現(xiàn)類都應(yīng)該提供 4 個(gè)“標(biāo)準(zhǔn)”構(gòu)造方法:1) void(無參數(shù))構(gòu)造方法,它創(chuàng)建一個(gè)空的有序映射,按照鍵的自然順序進(jìn)行排序。2) 帶有一個(gè) Comparator 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個(gè)空的有序映射,根據(jù)指定的比較器進(jìn)行排序。3) 帶有一個(gè) Map 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個(gè)新的有序映射,其鍵-值映射關(guān)系與參數(shù)相同,按照鍵的自然順序進(jìn)行排序。4) 帶有一個(gè) SortedMap 類型參數(shù)的構(gòu)造方法,它創(chuàng)建一個(gè)新的有序映射,其鍵-值映射關(guān)系和排序方法與輸入的有序映射相同。無法保證強(qiáng)制實(shí)施此建議,因?yà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: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 欧洲美女大片免费播放器视频| xxxxx做受大片在线观看免费 | 日本黄色免费观看| 999国内精品永久免费观看| 97久久国产亚洲精品超碰热| 免费一级国产生活片| 亚洲春色在线观看| 四虎AV永久在线精品免费观看| 亚洲欧洲日韩极速播放| 亚洲免费日韩无码系列| 中文字幕在线观看免费视频| 色噜噜噜噜亚洲第一| 亚洲制服中文字幕第一区| 免费看的黄色大片| 在线观看人成视频免费无遮挡| 中中文字幕亚洲无线码| 亚洲熟女一区二区三区| 啦啦啦在线免费视频| 天天爽亚洲中文字幕| 亚洲日韩v无码中文字幕| 成年美女黄网站色大免费视频| 毛片基地看看成人免费| 亚洲中文无码mv| 免费无码黄十八禁网站在线观看| 国产免费久久久久久无码| 在线综合亚洲中文精品| 国产亚洲成av片在线观看| 最近中文字幕免费mv视频7| 大地影院MV在线观看视频免费| 亚洲AV无码专区在线厂| 亚洲欧洲日韩在线电影| 日本亚洲成高清一区二区三区| 国产嫩草影院精品免费网址| 波多野结衣中文字幕免费视频| 久青草视频97国内免费影视| 相泽南亚洲一区二区在线播放| 亚洲人成电影在线观看青青| 亚洲av无码国产精品夜色午夜| 久久亚洲精品无码播放| 国产高清免费的视频| 在线v片免费观看视频|