方式一:
package collectionsdemo;
import java.util.Comparator;
import java.util.TreeSet;
public class CollectionsDemo implements Comparator <String>{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
demo();
}
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
int temp=o2.compareTo(o1);
return temp;
}
private static void demo() {
// TODO Auto-generated method stub
TreeSet ts=new TreeSet(new CollectionsDemo());
ts.add("dsadas");
ts.add("asss");
ts.add("abc");
ts.add("zzz");
System.out.println(ts);
}
}
方式二:
package collectionsdemo;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class CollectionsDemo{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
demo();
}
private static void demo() {
// TODO Auto-generated method stub
TreeSet ts=new TreeSet(Collections.reverseOrder());
ts.add("dsadas");
ts.add("asss");
ts.add("abc");
ts.add("zzz");
System.out.println(ts);
}
}