public abstract class CollationKey extends Object implements Comparable<CollationKey>
Collatorobject. Comparing two
CollationKeyinstances returns the relative order of the strings they represent.
Since the rule set of collators can differ, the sort orders of the same
string under two different
Collator instances might differ. Hence
comparing collation keys generated from different
can give incorrect results.
Both the method
CollationKey.compareTo(CollationKey) and the method
Collator.compare(String, String) compares two strings and returns
their relative order. The performance characteristics of these two approaches
During the construction of a
CollationKey, the entire source string
is examined and processed into a series of bits terminated by a null, that
are stored in the
CollationKey.compareTo(CollationKey) executes, it performs bitwise
comparison on the bit sequences. This can incur startup cost when creating
CollationKey, but once the key is created, binary comparisons
are fast. This approach is recommended when the same strings are to be
compared over and over again.
On the other hand, implementations of
Collator.compare(String, String) can examine and process the strings
only until the first characters differ in order. This approach is
recommended if the strings are to be compared only once.
The following example shows how collation keys can be used to sort a list of strings:
// Create an array of CollationKeys for the Strings to be sorted. Collator myCollator = Collator.getInstance(); CollationKey keys = new CollationKey; keys = myCollator.getCollationKey("Tom"); keys = myCollator.getCollationKey("Dick"); keys = myCollator.getCollationKey("Harry"); sort(keys);
// Inside body of sort routine, compare keys this way if( keys[i].compareTo( keys[j] ) > 0 ) // swap keys[i] and keys[j]
// Finally, when we've returned from sort. System.out.println(keys.getSourceString()); System.out.println(keys.getSourceString()); System.out.println(keys.getSourceString());
|Modifier||Constructor and Description|
|Modifier and Type||Method and Description|
Compares this collation key to the given collation key.
Returns the string from which this collation key was created.
Returns this collation key as a byte array.
protected CollationKey(String source)
public abstract int compareTo(CollationKey value)
public String getSourceString()
public abstract byte toByteArray()