All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.SortKey

java.lang.Object
   |
   +----java.text.SortKey

public final class SortKey
extends Object
Sort keys are generated by the Collation class. Use the SortKey objects instead of Collation to compare strings multiple times. A SortKey preprocesses the comparison information from the Collation object to make the comparison faster. If you are not going to comparing strings multiple times, then using the Collation object is generally faster, since it only processes as much of the string as needed to make a comparison.

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().

See Also:
Collation, TableCollation

Method Index

 o compareTo(SortKey)
Convenience method which does a string(bit-wise) comparison of the two sort keys.
 o equals(Object)
Compare if two objects are the same.
 o hashCode()
Creates an integer that is unique to the sort key.

Methods

 o compareTo
  public byte compareTo(SortKey target)
Convenience method which does a string(bit-wise) comparison of the two sort keys.

Parameters:
target - target sort key
Returns:
Returns LESS if sourceKey < targetKey, GREATER if sourceKey > targetKey and EQUAL otherwise.
See Also:
compareTo
 o equals
  public boolean equals(Object source)
Compare if two objects are the same.

Parameters:
source - the object to compare to.
Returns:
Returns true if two objects are equal, false otherwise.
Overrides:
equals in class Object
 o hashCode
  public int hashCode()
Creates an integer that is unique to the sort key. NOTE: this is not the same as String.hashCode, which is a call on an object. However, if you are building a hashTable, you must ensure that hash & equal are consistent, otherwise you will corrupt your structure. If two objects are identical, the hash values should be the same. That is, if x.equals(y), then x.hashCode() = y.hashCode(). This call is used by CollatedString.hashCode().

Example of use:

     Collation myCollation = Collation.getDefault(Locale.US);
     SortKey key1 = myCollation.getSortKey("abc");
     SortKey key2 = myCollation.getSortKey("ABC");
     // key1.hashCode() != key2.hashCode()
 

Returns:
the hash value based on the string's collation order.
Overrides:
hashCode in class Object
See Also:
hashCode

All Packages  Class Hierarchy  This Package  Previous  Next  Index