Thursday, September 29, 2011

USEFUL JAVA COMPARATOR CODE TO SORT THE TREESET DATA IN EITHER ORDER



import java.util.*;


class MyComparator implements Comparator {


public int compare(String a, String b) {


String str1, str2;
str1 = a;
str2 = b;
return str2.compareTo(str1);


}
}


class Engine {


public static void main(String rk[]) {


TreeSet ts = new TreeSet(new MyComparator());


ts.add("GAURANG");
ts.add("PARTH");
ts.add("HITEN");
ts.add("KRUNAL");
ts.add("NISHIDH");
ts.add("MANSUKH");
ts.add("KANDARP");
ts.add("SAMEER");
ts.add("SAMSOL");

for(String name: ts){
System.out.print(name + " ");
System.out.println();
}

}
}

No comments:

Post a Comment