All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----java.text.SortKey
For example (with strength == tertiary)
When comparing "Abernathy" to "Baggins-Smythworthy", Collation only needs to process a couple of characters, while a comparison with SortKeys will process all of the characters. On the other hand, if you are doing a sort of a number of fields, it is much faster to use SortKeys, since you will be comparing strings multiple times.
Typical use of SortKeys are in databases, where you store a SortKey in a hidden field, and use it for sorting or indexing.
Example of use:
Collation myCollation = Collation.getDefault(Locale.FRANCE);
String sortlist[8] = { "abc", "äbc", "ÄBC", "Äbc",
"resume", "résumé", "RESUME","Résumé" };
SortKey sortKeys[8];
myCollation.setStrength(Collation.SECONDARY);
int i;
for (i = 0; i < 8; i++)
sortKeys[i] = myCollation.getSortKey(sortlist[i]);
Hashtable sortkeyTable = new Hashtable();
for (i = 0; i < 8; i++)
sortkeyTable.put(sortlist[i], sortKeys[i]);
// query the hash table with "RESUME", "résumé", and "Résumé"
// will return the objects that compares equal
SortKey sortkey1 = (SortKey)sortkeyTable.get("RESUME");
SortKey sortkey2 = (SortKey)sortkeyTable.get("résumé");
SortKey sortkey3 = (SortKey)sortkeyTable.get("Résumé");
if ((sortkey1.equals(sortkey2) &&
(sortkey2.equals(sortkey3))
System.out.println("Test passes!");
Because Collation.compare()'s algorithm is complex, it is faster to sort
long lists of words by retrieving sort keys with Collation.getSortKey().
You can then cache the sort keys and compare them using SortKey.compareTo().
public byte compareTo(SortKey target)
public boolean equals(Object source)
public int hashCode()
Example of use:
Collation myCollation = Collation.getDefault(Locale.US);
SortKey key1 = myCollation.getSortKey("abc");
SortKey key2 = myCollation.getSortKey("ABC");
// key1.hashCode() != key2.hashCode()
All Packages Class Hierarchy This Package Previous Next Index