- 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
- 程序来源:http://mindprod.com/jgloss/comparator.html
- 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);
- }
- }
Comparator 使用小结
最新推荐文章于 2024-05-30 20:22:14 发布