Comparator 使用小结

本文介绍了 Java 中 Comparator 接口的基本概念及其使用方法。详细解释了如何通过自定义 Comparator 对象来对集合中的元素进行排序,并提供了排序中文字符串的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. java.util.Comparator   
  2.     
  3. 1:java.util.Comparator是一个接口,只包含两个方法:   
  4.   
  5. 方法摘要   
  6.  int  
  7.        
  8. compare(T o1, T o2)   
  9.           比较用来排序的两个参数。   
  10.  boolean  
  11.        
  12. equals(Object obj)   
  13.           指示是否其他对象“等于”此 Comparator。   
  14.     
  15. 2:在JDK中对java.util.Comparator的功能的解释为:“强行对某些对象 collection 进行整体排序”。   
  16. 具体的实现是在compare(T o1, T o2) 方法中自定义排序算法,然后将Comparator对象(实现了java.util.Comparator接口的对象)作为一个参数传递给欲排序对象 collection的某些排序方法。某些排序方法指的是能够接受java.util.Comparator参数的方法,比如:java.util. Arrays的public static void sort(Object[] a, Comparator c)   
  17.     
  18. 3:典型例程:   
  19.   
  20. import java.util.Comparator;   
  21.   
  22.     
  23.   
  24. public class ByWeightComparator implements Comparator   
  25.   
  26.    {   
  27.   
  28.     
  29.   
  30.    /**  
  31.  
  32.    * Compare two Trucks by weight. Callback for sort or TreeMap.  
  33.  
  34.    * effectively returns a-b; orders by ascending weight  
  35.  
  36.    *  
  37.  
  38.    * @param pFirst first object a to be compared  
  39.  
  40.    *  
  41.  
  42.    * @param pSecond second object b to be compared  
  43.  
  44.    *  
  45.  
  46.    * @return +1 if a>b, 0 if a=b, -1 if a<b  
  47.  
  48.    */  
  49.   
  50.    public final int compare ( Object pFirst, Object pSecond )   
  51.   
  52.       {   
  53.   
  54.       long aFirstWeight = ( (Truck)pFirst ).weight;   
  55.   
  56.       long aSecondWeight = ( (Truck)pSecond ).weight;   
  57.   
  58.       /* need signum to convert long to int, (int)will not do! */  
  59.   
  60.       return signum( aFirstWeight - aSecondWeight );   
  61.   
  62.       } // end compare   
  63.   
  64.    /**  
  65.  
  66.    * Collapse number down to +1 0 or -1 depending on sign.  
  67.  
  68.    * Typically used in compare routines to collapse a difference  
  69.  
  70.    * of two longs to an int.  
  71.  
  72.    *  
  73.  
  74.    * @param diff usually represents the difference of two long.  
  75.  
  76.    *  
  77.  
  78.    * @return signum of diff, +1, 0 or -1.  
  79.  
  80.    */  
  81.   
  82.    public static final int signum ( long diff )   
  83.   
  84.       {   
  85.   
  86.       if ( diff > 0 ) return 1;   
  87.   
  88.       if ( diff < 0 ) return -1;   
  89.   
  90.       else return 0;   
  91.   
  92.       } // end signum   
  93.   
  94.     
  95.   
  96.    } // end class ByWeight   
  97. 程序来源:http://mindprod.com/jgloss/comparator.html   
  98.     
  99.   
  100. 4:以下程序演示了如何对自定义对象进行排序:   
  101.   
  102. package mypack;   
  103.   
  104.     
  105.   
  106. import java.util.Arrays;   
  107.   
  108. import java.util.Comparator;   
  109.   
  110.     
  111.   
  112. public class ComparatorTest {   
  113.   
  114.     
  115.   
  116.     @SuppressWarnings("unchecked")   
  117.   
  118.     public static void main(String[] args) {   
  119.   
  120.        Dog o1 = new Dog("dog1"15);   
  121.   
  122.        Dog o2 = new Dog("dog2"24);   
  123.   
  124.        Dog o3 = new Dog("dog3"33);   
  125.   
  126.        Dog o4 = new Dog("dog4"42);   
  127.   
  128.        Dog o5 = new Dog("dog5"51);   
  129.   
  130.     
  131.   
  132.        Dog[] dogs = new Dog[] { o1, o4, o3, o5, o2 };   
  133.   
  134.     
  135.   
  136.        System.out.println("未排序前");   
  137.   
  138.        for (int i = 0; i < dogs.length; i++) {   
  139.   
  140.            Dog dog = dogs[i];   
  141.   
  142.            System.out.println(dog.getName());   
  143.   
  144.        }   
  145.   
  146.     
  147.   
  148.        Arrays.sort(dogs, new ByHeightComparator());   
  149.   
  150.        System.out.println("使用高度排序之后:");   
  151.   
  152.        for (int i = 0; i < dogs.length; i++) {   
  153.   
  154.            Dog dog = dogs[i];   
  155.   
  156.            System.out.println(dog.getName());   
  157.   
  158.        }   
  159.   
  160.     
  161.   
  162.        Arrays.sort(dogs, new ByWeightComparator());   
  163.   
  164.        System.out.println("使用重量排序之后:");   
  165.   
  166.        for (int i = 0; i < dogs.length; i++) {   
  167.   
  168.            Dog dog = dogs[i];   
  169.   
  170.            System.out.println(dog.getName());   
  171.   
  172.        }   
  173.   
  174.     
  175.   
  176.     }   
  177.   
  178. }   
  179.   
  180.     
  181.   
  182. class Dog {   
  183.   
  184.     
  185.   
  186.     private String name;   
  187.   
  188.     
  189.   
  190.     private int weight;   
  191.   
  192.     
  193.   
  194.     private int height;   
  195.   
  196.     
  197.   
  198.     public Dog(String name, int weight, int height) {   
  199.   
  200.        this.setName(name);   
  201.   
  202.        this.weight = weight;   
  203.   
  204.        this.height = height;   
  205.   
  206.     }   
  207.   
  208.     
  209.   
  210.     public int getHeight() {   
  211.   
  212.        return height;   
  213.   
  214.     }   
  215.   
  216.     
  217.   
  218.     public void setHeight(int height) {   
  219.   
  220.        this.height = height;   
  221.   
  222.     }   
  223.   
  224.     
  225.   
  226.     public int getWeight() {   
  227.   
  228.        return weight;   
  229.   
  230.     }   
  231.   
  232.     
  233.   
  234.     public void setWeight(int weight) {   
  235.   
  236.        this.weight = weight;   
  237.   
  238.     }   
  239.   
  240.     
  241.   
  242.     public void setName(String name) {   
  243.   
  244.        this.name = name;   
  245.   
  246.     }   
  247.   
  248.     
  249.   
  250.     public String getName() {   
  251.   
  252.        return name;   
  253.   
  254.     }   
  255.   
  256. }   
  257.   
  258.     
  259.   
  260. class ByWeightComparator implements Comparator {   
  261.   
  262.     
  263.   
  264.     public int compare(Object firstDog, Object secondDog) {   
  265.   
  266.        int firstWeight = ((Dog) firstDog).getWeight();   
  267.   
  268.        int secondWeight = ((Dog) secondDog).getWeight();   
  269.   
  270.        return signum(firstWeight - secondWeight);   
  271.   
  272.     }   
  273.   
  274.     
  275.   
  276.     public static final int signum(int diff) {   
  277.   
  278.        if (diff > 0)   
  279.   
  280.            return 1;   
  281.   
  282.        if (diff < 0)   
  283.   
  284.            return -1;   
  285.   
  286.        else  
  287.   
  288.            return 0;   
  289.   
  290.     }   
  291.   
  292.     
  293.   
  294. }   
  295.   
  296.     
  297.   
  298. class ByHeightComparator implements Comparator {   
  299.   
  300.     
  301.   
  302.     public int compare(Object firstDog, Object secondDog) {   
  303.   
  304.        int firstHeight = ((Dog) firstDog).getHeight();   
  305.   
  306.        int secondHeight = ((Dog) secondDog).getHeight();   
  307.   
  308.        return signum(firstHeight - secondHeight);   
  309.   
  310.     }   
  311.   
  312.     
  313.   
  314.     public static final int signum(int diff) {   
  315.   
  316.        if (diff > 0)   
  317.   
  318.            return 1;   
  319.   
  320.        if (diff < 0)   
  321.   
  322.            return -1;   
  323.   
  324.        else  
  325.   
  326.            return 0;   
  327.   
  328.     }   
  329.   
  330.     
  331.   
  332. }   
  333.   
  334. 5:以下程序演示了如何对中文进行排序   
  335.   
  336. package mypack;   
  337.   
  338.     
  339.   
  340. import java.text.Collator;   
  341.   
  342. import java.text.RuleBasedCollator;   
  343.   
  344. import java.util.Arrays;   
  345.   
  346. import java.util.Locale;   
  347.   
  348.     
  349.   
  350. public class Test {   
  351.   
  352.     public static void main(String[] args) {   
  353.   
  354.        String[] test = new String[] { "的""波""次""啊"};   
  355.   
  356.        Arrays.sort(test, (RuleBasedCollator) Collator.getInstance(Locale.CHINA));   
  357.   
  358.        for (String key : test)   
  359.   
  360.            System.out.println(key);   
  361.   
  362.     }   
  363.   
  364.     
  365.   
  366. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值