日期:2014-05-20 浏览次数:20869 次
import java.util.*; public class TreeSetDemo { public static void main(String[] args) { Comparator<String> comparator = new CustomComparator1<String>(); Set<String> set = new TreeSet<String>(comparator); set.add("justin"); set.add("caterpillar"); set.add("momor"); for(String name : set) { System.out.print(name + " "); } System.out.println(); } }
import java.util.Comparator; public class CustomComparator1<T> implements Comparator<T> { public int compare(T o1, T o2) { if (((T) o1).equals(o2)) return 0; return ((Comparable<T>) o1).compareTo((T) o2) * -1; } }