• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            杰 & C++ & Python & DM

            Java Comparator和Comparabler的區別

              新接觸java沒多久,前幾天用到排序的問題,看到Comparator和Comparable兩個接口類有點迷惑,研究了兩天也沒理解有什么區別,今天在看《Java核心編程》時,才恍然大悟。在這里表達一下自己的想法。
              當需要排序的集合或數組時可以使用Comparator或Comparable,它們都可以實現排序,但是它們的區別是Comparator從外部定義了對象的比較規則,而Comparable則是從內部定義了對象是可比較的。下面將詳細解這句話。

            一、 Comparator
              Comparator從外部定義了對象的比較規則
              比如,你要使用某人寫的一個矩形類Rect。現在你有一個Rect的集合(或數組),你想實現對Rect的排序,現在有一個問題,某人在實現Rect的時候沒有考慮到會有人將會比較Rect對象。這個時候你必須根據需要對Rect進行排序(比如,根據矩形的長進行排序),在這個場景下使用Comparator,因為Rect類已經存在,你不能對其進行改變。

            import java.util.*;

            public class Rectangle {
                
                
            public static void main(String[] args)
                {
                    Rect[] rectArrays 
            = new Rect[] {new Rect(34), new Rect(52), new Rect(45)};
                    
                    
            // 排序,將定義的RectComparator作為參數
                    Arrays.sort(rectArrays, new RectComparator());
                    
                    
            for (int i=0; i != rectArrays.length; ++i)
                        System.out.println(rectArrays[i]);
                }
                
                
            // 定義一個Rect比較方式:根據Rect的長比較
                public static class RectComparator implements Comparator<Rect>
                {
                    
            public int compare(Rect o1, Rect o2)
                    {
                        
            return o1.getLength() - o2.getLength();
                    }
                }

                
            public static class Rect
                {
                    Rect(
            int l, int w)
                    {
                        
            this.length = l;
                        
            this.width = w;
                    }
                    
                    
            public int getLength()
                    {
                        
            return this.length;
                    }
                    
                    
            public int getWidth()
                    {
                        
            return this.width;
                    }
                    
                    
            public int getArea()
                    {
                        
            return this.length * this.width;
                    }
                    
                    
            public String toString()
                    {
                        
            return "length: " + length + " width: " + width;
                    }
                    
                    
            private int length;
                    
            private int width;
                }
            }

            輸出:
            length: 3 width: 4
            length: 4 width: 5
            length: 5 width: 2


            二、 Comparable
              Comparable則是從內部定義了對象的是可比較的
              還是以Rect為例,假如你是Rect的實現者,在你定義Rect時,你覺得有必要定義一個比較方式,這個時候就應該使Rect繼承Comparable接口。如果你覺得較合理的排序方式是根據Rect的面積進行排序,那么可以這樣實現

            import java.util.*;

            public class Rectangle {
                
                
            public static void main(String[] args)
                {
                    Rect[] rectArrays 
            = new Rect[] {new Rect(34), new Rect(52), new Rect(45)};
                    
                    Arrays.sort(rectArrays);
                    
                    
            for (int i=0; i != rectArrays.length; ++i)
                        System.out.println(rectArrays[i]);
                }

                
            // 定義了Comparable接口
                public static class Rect implements Comparable<Rect>
                {
                    Rect(
            int l, int w)
                    {
                        
            this.length = l;
                        
            this.width = w;
                    }
                    
                    
            public int getLength()
                    {
                        
            return this.length;
                    }
                    
                    
            public int getWidth()
                    {
                        
            return this.width;
                    }
                    
                    
            public int getArea()
                    {
                        
            return this.length * this.width;
                    }
                    
                    
            public String toString()
                    {
                        
            return "length: " + length + " width: " + width;
                    }
                    
                    
            // 重載compareTo函數,按面積比較
                    @Override
                    
            public int compareTo(Rect that)
                    {
                        
            return this.getArea() - that.getArea();
                    }
                    
                    
            private int length;
                    
            private int width;
                }
            }
            輸出:
            length: 5 width: 2
            length: 3 width: 4
            length: 4 width: 5

            三、總結
            通過Comparator和Comparable的意思我們也可以看出兩者的區別
            Comparable意為“可比較的”,一個類繼承了Camparable接口則表明這個類的對象之間是可以相互比較的,這個類對象組成的集合就可以直接使用sort方法排序。
            Comparator意為“比較算子”,因此Comparator可以看成一種算法的實現,將算法和數據分離。

              另外,通過定義方式,我們可以發現如果一個類繼承了Comparable接口,則表明這個類的對象之間是可以比較的,且比較的方式只有一種。但是Comparator可以定義多種比較方式。在第二個程序中,Rect定義了按面積進行比較,如果我們想按長對Rect進行排序,那么也可以通過Comparator來實現。

              最后,再次強調Comparator從外部定義了對象的比較規則,而Comparable則是從內部定義了對象是可比較的


            參考資料
            http://www.blogjava.net/fastunit/archive/2008/04/08/191533.html
            《Java核心編程》第7版,p91-p92





            posted on 2012-04-11 16:04 jaysoon 閱讀(1196) 評論(0)  編輯 收藏 引用 所屬分類: Java

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            收藏夾

            C++

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲欧美精品一区久久中文字幕| 国产aⅴ激情无码久久| 国产99精品久久| 久久99国产精品久久| 久久电影网| 99久久国产精品免费一区二区 | 伊人久久大香线蕉综合Av| 久久婷婷五月综合色奶水99啪| 精品国产一区二区三区久久| 精品99久久aaa一级毛片| 日批日出水久久亚洲精品tv| 婷婷久久久亚洲欧洲日产国码AV| 久久国产精品国产自线拍免费 | 精品久久人妻av中文字幕| 97久久精品人人澡人人爽| 久久久久波多野结衣高潮| 99re久久精品国产首页2020| 亚洲国产成人久久精品99 | 99久久国产综合精品网成人影院| 久久久精品波多野结衣| 久久免费的精品国产V∧| 一本久久精品一区二区| 亚洲国产精品久久久久久| 午夜天堂av天堂久久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 亚洲国产美女精品久久久久∴| 99久久精品九九亚洲精品| 人妻无码中文久久久久专区| 亚洲国产精品无码久久九九| 国产精品美女久久久久av爽| 久久香蕉国产线看观看精品yw| 奇米影视7777久久精品人人爽| 久久av高潮av无码av喷吹| 国产精品免费福利久久| 久久亚洲私人国产精品| 国内高清久久久久久| 久久精品国产亚洲AV蜜臀色欲| 无码精品久久一区二区三区| 日韩AV毛片精品久久久| 久久综合日本熟妇| 三级韩国一区久久二区综合|