• <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++

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品久久久一二三区| 免费精品久久久久久中文字幕| 久久国产美女免费观看精品 | 伊人久久综合成人网| 久久精品国产影库免费看| 国产福利电影一区二区三区久久久久成人精品综合 | 成人精品一区二区久久| 国产精品青草久久久久婷婷| 亚洲国产精品综合久久网络| 国产精品一久久香蕉国产线看观看 | 香蕉久久久久久狠狠色| 久久国产亚洲精品无码| 亚洲国产精品综合久久网络 | 久久久亚洲AV波多野结衣| 国产成人久久精品麻豆一区| 亚洲国产成人乱码精品女人久久久不卡 | 久久国产精品久久| 久久夜色精品国产| 麻豆成人久久精品二区三区免费| 久久久久久亚洲精品影院| 国产精品美女久久久久网| 伊人久久精品无码二区麻豆| 久久久青草青青国产亚洲免观| 国内精品久久久久久99| 国产精品久久久香蕉| 久久人妻少妇嫩草AV无码蜜桃| 久久精品国产免费| 久久久无码精品亚洲日韩按摩| 久久91精品国产91| 无码任你躁久久久久久久| 精品久久综合1区2区3区激情| 97超级碰碰碰碰久久久久| 国产伊人久久| 9191精品国产免费久久| 久久A级毛片免费观看| 人妻无码中文久久久久专区| 久久久国产精华液| 性欧美丰满熟妇XXXX性久久久 | 久久亚洲国产最新网站| 亚洲伊人久久综合中文成人网| 国产成人精品久久一区二区三区av|