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

}
}

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in J2EE. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s