Comparator使用小结

java.util.Comparator
 
1:java.util.Comparator是一个接口,只包含两个方法:

方法摘要
 int
compare( T o1, T o2)
          比较用来排序的两个参数。
 boolean
equals( Object obj)
          指示是否其他对象“等于”此 Comparator。

 
2:在JDK中对java.util.Comparator的功能的解释为:“强行对某些对象 collection 进行整体排序”。
具体的实现是在 compare( T o1, T o2)方法中自定义排序算法,然后将Comparator对象(实现了java.util.Comparator接口的对象)作为一个参数传递给欲排序对象collection的某些排序方法。某些排序方法指的是能够接受java.util.Comparator参数的方法,比如:java.util. Arrays的public static void sort(Object[] a, Comparator c)
 
3:典型例程:

import java.util.Comparator;

 

public class ByWeightComparator implements Comparator

   {

 

   /**

   * Compare two Trucks by weight. Callback for sort or TreeMap.

   * effectively returns a-b; orders by ascending weight

   *

   * @param pFirst first object a to be compared

   *

   * @param pSecond second object b to be compared

   *

   * @return +1 if a>b, 0 if a=b, -1 if a<b

   */

   public final int compare ( Object pFirst, Object pSecond )

      {

      long aFirstWeight = ( (Truck)pFirst ).weight;

      long aSecondWeight = ( (Truck)pSecond ).weight;

      /* need signum to convert long to int, (int)will not do! */

      return signum( aFirstWeight - aSecondWeight );

      } // end compare

   /**

   * Collapse number down to +1 0 or -1 depending on sign.

   * Typically used in compare routines to collapse a difference

   * of two longs to an int.

   *

   * @param diff usually represents the difference of two long.

   *

   * @return signum of diff, +1, 0 or -1.

   */

   public static final int signum ( long diff )

      {

      if ( diff > 0 ) return 1;

      if ( diff < 0 ) return -1;

      else return 0;

      } // end signum

 

   } // end class ByWeight

 

4:以下程序演示了如何对自定义对象进行排序:

package mypack;

 

import java.util.Arrays;

import java.util.Comparator;

 

public class ComparatorTest {

 

    @SuppressWarnings("unchecked")

    public static void main(String[] args) {

       Dog o1 = new Dog("dog1", 1, 5);

       Dog o2 = new Dog("dog2", 2, 4);

       Dog o3 = new Dog("dog3", 3, 3);

       Dog o4 = new Dog("dog4", 4, 2);

       Dog o5 = new Dog("dog5", 5, 1);

 

       Dog[] dogs = new Dog[] { o1, o4, o3, o5, o2 };

 

       System.out.println("未排序前");

       for (int i = 0; i < dogs.length; i++) {

           Dog dog = dogs[i];

           System.out.println(dog.getName());

       }

 

       Arrays.sort(dogs, new ByHeightComparator());

       System.out.println("使用高度排序之后:");

       for (int i = 0; i < dogs.length; i++) {

           Dog dog = dogs[i];

           System.out.println(dog.getName());

       }

 

       Arrays.sort(dogs, new ByWeightComparator());

       System.out.println("使用重量排序之后:");

       for (int i = 0; i < dogs.length; i++) {

           Dog dog = dogs[i];

           System.out.println(dog.getName());

       }

 

    }

}

 

class Dog {

 

    private String name;

 

    private int weight;

 

    private int height;

 

    public Dog(String name, int weight, int height) {

       this.setName(name);

       this.weight = weight;

       this.height = height;

    }

 

    public int getHeight() {

       return height;

    }

 

    public void setHeight(int height) {

       this.height = height;

    }

 

    public int getWeight() {

       return weight;

    }

 

    public void setWeight(int weight) {

       this.weight = weight;

    }

 

    public void setName(String name) {

       this.name = name;

    }

 

    public String getName() {

       return name;

    }

}

 

class ByWeightComparator implements Comparator {

 

    public int compare(Object firstDog, Object secondDog) {

       int firstWeight = ((Dog) firstDog).getWeight();

       int secondWeight = ((Dog) secondDog).getWeight();

       return signum(firstWeight - secondWeight);

    }

 

    public static final int signum(int diff) {

       if (diff > 0)

           return 1;

       if (diff < 0)

           return -1;

       else

           return 0;

    }

 

}

 

class ByHeightComparator implements Comparator {

 

    public int compare(Object firstDog, Object secondDog) {

       int firstHeight = ((Dog) firstDog).getHeight();

       int secondHeight = ((Dog) secondDog).getHeight();

       return signum(firstHeight - secondHeight);

    }

 

    public static final int signum(int diff) {

       if (diff > 0)

           return 1;

       if (diff < 0)

           return -1;

       else

           return 0;

    }

 

}

5:以下程序演示了如何对中文进行排序

package mypack;

 

import java.text.Collator;

import java.text.RuleBasedCollator;

import java.util.Arrays;

import java.util.Locale;

 

public class Test {

    public static void main(String[] args) {

       String[] test = new String[] { "", "", "", ""};

       Arrays.sort(test, (RuleBasedCollator) Collator.getInstance(Locale.CHINA));

       for (String key : test)

           System.out.println(key);

    }

 

}

 

详细信息参见: http://www.javaresearch.org/article/53609.htm
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值