Sorting irrespective of case in java:
In Java if you want to sort the elements then you can use treeset. Set will not allow duplicates, but if you have the same element in both small and upper case then it will consider that as separate elements.
Sorting respective of case:
[java]
import java.util.Set;
import java.util.TreeSet;
public class Sorting {
/**
* @param args
*/
public static void main(String[] args) {
Set<String> mySet = new TreeSet<String>();
mySet.add("Vizag");
mySet.add("arcot");
mySet.add("Vellore");
mySet.add("delhi");
mySet.add("Bangalore");
mySet.add("chennai");
for (String string : mySet) {
System.out.println(string);
}
}
}
[/java]
Output:
[plain]
Arcot
Bangalore
Vellore
Vizag
arcot
chennai
delhi
[/plain]
here, sorting happended only with capital letters first then small letter sorting happended. If you want to sorting irrespective of case then use “CASE_INSENSITIVE_ORDER”.
Sorting irrespective of case:
If we add the “String.CASE_INSENSITIVE_ORDER” then it will remove the duplicates and sort in combined caps/small order.
[java]
import java.util.Set;
import java.util.TreeSet;
public class Sorting {
/**
* @param args
*/
public static void main(String[] args) {
Set<String> mySet = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
mySet.add("Vizag");
mySet.add("arcot");
mySet.add("Vellore");
mySet.add("delhi");
mySet.add("Bangalore");
mySet.add("chennai");
mySet.add("Arcot");
for (String string : mySet) {
System.out.println(string);
}
}
}
[/java]
Output:
[plain]
arcot
Bangalore
chennai
delhi
Vellore
Vizag
[/plain]
Java Recommended Books – Collections:
Feel free to share your comments/feedbacks/suggestions….