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);
}
}