|
java API针对集合类型排序提供了两种支持:java.util.Collections.sort(java.util.List)
java.util.Collections.sort(java.util.List, java.util.Comparator)
第一个方法要求所排序的元素类必须实现java.lang.Comparable接口。
第二个方法要求实现一个java.util.Comparator接口。java.lang.Comparable接口和java.util.Comparator接口是Java对排序最提供最基本支持。这两个接口不但可以用于集合元素排序,还可以用于数组排序。如果数组或集合元素是String类型,则可以利用Java API实现的Comparator<String>对象String.CASE_INSENSITIVE_ORDER为容器元素排序。下面给出两个里测试,涵盖集合和数组的排序,并且还演示了数组和集合的相互转换:例子一:实现Comparable接口排序package collsort.comparable;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-3-29 22:21:19
* Company: LavaSoft(http://lavasoft.blog.51cto.com)
* 要排序的元素对象
*/
public class Cat implements Comparable<Cat> {
private int age;
private String name;
public Cat(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Cat{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public int compareTo(Cat o) {
return this.getAge() - o.getAge();
}
}
package collsort.comparable;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-3-29 22:24:12
* Company: LavaSoft(http://lavasoft.blog.51cto.com)
* 通过实现Comparable接口实现个性化排序测试
*/
public class TestComparable {
public static String outCollection(Collection coll) {
StringBuffer sb = new StringBuffer();
for (Object obj : coll) {
sb.append(obj + "\n");
}
System.out.println(sb.toString());
return sb.toString();
}
public static void main(String args[]) {
test();
test2();
}
public static void test() {
System.out.println("----------test()---------");
System.out.println("升序排序测试:");
List<Cat> listCat = new ArrayList<Cat>();
Cat cat1 = new Cat(34, "hehe");
Cat cat2 = new Cat(12, "haha");
// Person catx = new Person(12, "lavasoft");
Cat cat3 = new Cat(23, "leizhimin");
Cat cat4 = new Cat(13, "lavasoft");
listCat.add(cat1);
listCat.add(cat2);
listCat.add(cat3);
// listCat.add(catx);
System.out.println("原集合为:");
outCollection(listCat);
System.out.println("调用Collections.sort(List<T> list)排序:");
Collections.sort(listCat);
outCollection(listCat);
System.out.println("逆序排列元素:");
Collections.sort(listCat, Collections.reverseOrder());
outCollection(listCat);
System.out.println("再次逆序排列元素:");
Collections.reverse(listCat);
outCollection(listCat);
System.out.println("添加一个元素后输出集合:");
listCat.add(cat4);
outCollection(listCat);
System.out.println("排列后输出:");
Collections.sort(listCat);
outCollection(listCat);
}
/**
* 针对数组的排序
*/
public static void test2(){
String[] strArray = new String[] {"z", "a", "C"};
System.out.println("-------------数组转换为列表-------------");
List<String> list = Arrays.asList(strArray);
outCollection(list);
System.out.println("-------------列表转换为数组(1)-------------");
String[] strArrayNew1 = list.toArray(strArray);
for(String str:strArrayNew1){
System.out.println(str);
}
System.out.println("-------------列表转换为数组(2)-------------");
String[] strArrayNew2 = (String[]) list.toArray();
for(String str:strArrayNew2){
System.out.println(str);
}
System.out.println("-------------顺序排序列表-------------");
Collections.sort(list);
outCollection(list);
System.out.println("-----按String实现的Comparator对象String.CASE_INSENSITIVE_ORDER排序----");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
outCollection(list);
System.out.println("-------------倒序排序列表-------------");
Collections.sort(list, Collections.reverseOrder());
outCollection(list);
System.out.println("-----按String实现的Comparator对象String.CASE_INSENSITIVE_ORDER排序----");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
outCollection(list);
System.out.println("-----反转列表元素的顺序------");
Collections.reverse(list);
outCollection(list);
}
} |
|