Compare compareTo() function of String class with Collator and CollationKey
By: Riktesh Srivastava Printer Friendly Format
The documentation for the compareTo() method of String defines the ordering lexicographically. This means the comparison is of the numerical values of the characters in the text, which is not necessarily alphabetically in all languages. For locale-specific ordering, use Collator with CollationKey.
The following example demonstrates the use of Collator with CollationKey to do a locale-specific sorting:
import java.text.*;
import java.util.*;
public class program{
public static void main(String args[])
{
Collator collator = Collator.getInstance();
CollationKey key1 = collator.getCollationKey(\"riks\");
CollationKey key2 = collator.getCollationKey(\"riky\");
CollationKey key3 = collator.getCollationKey(\"riktesh\");
CollationKey key4 = collator.getCollationKey(\"Riky\");
CollationKey key5 = collator.getCollationKey(\"Riktesh\");
Set set = new TreeSet();
set.add(key1);
set.add(key2);
set.add(key3);
set.add(key4);
set.add(key5);
Iterator iterator = set.iterator();
System.out.print(\"[\");
while (iterator.hasNext())
{
CollationKey key = (CollationKey)iterator.next();
System.out.print(key.getSourceString());
System.out.print(\",\");
}
System.out.println(\"]\");
}
}
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Nice Article....
View Tutorial By: Vipin Joshi at 2008-04-13 15:35:42