public final class CollationKey extends Object implements Comparable<CollationKey>
CollationKey
represents a String
under the rules of a specific Collator
object. Comparing two CollationKey
s returns the
relative order of the String
s they represent.
Since the rule set of Collator
s can differ, the
sort orders of the same string under two different
Collator
s might differ. Hence comparing
CollationKey
s generated from different
Collator
s can give incorrect results.
Both the method
CollationKey.compareTo(CollationKey)
and the method
Collator.compare(String, String)
compare two strings
and returns their relative order. The performance characteristics
of these two approaches can differ.
Note that collation keys are often less efficient than simply doing comparison.
For more details, see the ICU User Guide.
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
.
When CollationKey.compareTo(CollationKey)
executes, it
performs bitwise comparison on the bit sequences. This can incurs
startup cost when creating the 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 differing in
order. This approach is recommended if the strings are to be
compared only once.
More information about the composition of the bit sequence can be found in the user guide.
The following example shows how CollationKey
s can be used
to sort a list of String
s.
// Create an array of CollationKeys for the Strings to be sorted. Collator myCollator = Collator.getInstance(); CollationKey[] keys = new CollationKey[3]; keys[0] = myCollator.getCollationKey("Tom"); keys[1] = myCollator.getCollationKey("Dick"); keys[2] = 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[0].getSourceString() ); System.out.println( keys[1].getSourceString() ); System.out.println( keys[2].getSourceString() );
This class is not subclassable
Collator
,
RuleBasedCollator
Modifier and Type | Class and Description |
---|---|
static class |
CollationKey.BoundMode
Options that used in the API CollationKey.getBound() for getting a
CollationKey based on the bound mode requested.
|
Constructor and Description |
---|
CollationKey(String source,
byte[] key)
CollationKey constructor.
|
CollationKey(String source,
RawCollationKey key)
CollationKey constructor that forces key to release its internal byte
array for adoption. key will have a null byte array after this
construction.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(CollationKey target)
Compare this CollationKey to another CollationKey.
|
boolean |
equals(CollationKey target)
Compare this CollationKey and the argument target CollationKey for
equality.
|
boolean |
equals(Object target)
Compare this CollationKey and the specified Object for
equality.
|
CollationKey |
getBound(int boundType,
int noOfLevels)
Produces a bound for the sort order of a given collation key and a
strength level.
|
String |
getSourceString()
Return the source string that this CollationKey represents.
|
int |
hashCode()
Returns a hash code for this CollationKey.
|
CollationKey |
merge(CollationKey source)
Merges this CollationKey with another.
|
byte[] |
toByteArray()
Duplicates and returns the value of this CollationKey as a sequence
of big-endian bytes terminated by a null.
|
public CollationKey(String source, byte[] key)
Collator.getCollationKey(String)
.source
- string this CollationKey is to representkey
- array of bytes that represent the collation order of argument
source terminated by a nullCollator
public CollationKey(String source, RawCollationKey key)
source
- string this CollationKey is to representkey
- RawCollationKey object that represents the collation order of
argument source.Collator
,
RawCollationKey
public String getSourceString()
public byte[] toByteArray()
If two CollationKeys can be legitimately compared, then one can compare the byte arrays of each to obtain the same result, e.g.
byte key1[] = collationkey1.toByteArray(); byte key2[] = collationkey2.toByteArray(); int key, targetkey; int i = 0; do { key = key1[i] & 0xFF; targetkey = key2[i] & 0xFF; if (key < targetkey) { System.out.println("String 1 is less than string 2"); return; } if (targetkey < key) { System.out.println("String 1 is more than string 2"); } i ++; } while (key != 0 && targetKey != 0); System.out.println("Strings are equal.");
public int compareTo(CollationKey target)
Note: Comparison between CollationKeys created by different Collators might return incorrect results. See class documentation.
compareTo
in interface Comparable<CollationKey>
target
- target CollationKeyNullPointerException
- is thrown if argument is null.Collator.compare(String, String)
public boolean equals(Object target)
See note in compareTo(CollationKey) for warnings about possible incorrect results.
equals
in class Object
target
- the object to compare to.ClassCastException
- is thrown when the argument is not
a CollationKey. NullPointerException is thrown when the argument
is null.compareTo(CollationKey)
public boolean equals(CollationKey target)
See note in compareTo(CollationKey) for warnings of incorrect results
target
- the CollationKey to compare to.NullPointerException
- is thrown when the argument is null.public int hashCode()
public CollationKey getBound(int boundType, int noOfLevels)
Resulting bounds can be used to produce a range of strings that are between upper and lower bounds. For example, if bounds are produced for a sortkey of string "smith", strings between upper and lower bounds with primary strength would include "Smith", "SMITH", "sMiTh".
There are two upper bounds that can be produced. If BoundMode.UPPER is produced, strings matched would be as above. However, if a bound is produced using BoundMode.UPPER_LONG is used, the above example will also match "Smithsonian" and similar.
For more on usage, see example in test procedure src/com/ibm/icu/dev/test/collator/CollationAPITest/TestBounds.
Collation keys produced may be compared using the compare API.
boundType
- Mode of bound required. It can be BoundMode.LOWER, which
produces a lower inclusive bound, BoundMode.UPPER, that
produces upper bound that matches strings of the same
length or BoundMode.UPPER_LONG that matches strings that
have the same starting substring as the source string.noOfLevels
- Strength levels required in the resulting bound
(for most uses, the recommended value is PRIMARY). This
strength should be less than the maximum strength of
this CollationKey.
See users guide for explanation on the strength levels a
collation key can have.IllegalArgumentException
- thrown when the strength level
requested is higher than or equal to the strength in this
CollationKey.
In the case of an Exception, information
about the maximum strength to use will be returned in the
Exception. The user can then call getBound() again with the
appropriate strength.CollationKey
,
CollationKey.BoundMode
,
Collator.PRIMARY
,
Collator.SECONDARY
,
Collator.TERTIARY
,
Collator.QUATERNARY
,
Collator.IDENTICAL
public CollationKey merge(CollationKey source)
This is useful, for example, for combining sort keys from first and last names to sort such pairs. See http://www.unicode.org/reports/tr10/#Merging_Sort_Keys
The recommended way to achieve "merged" sorting is by concatenating strings with U+FFFE between them. The concatenation has the same sort order as the merged sort keys, but merge(getSortKey(str1), getSortKey(str2)) may differ from getSortKey(str1 + '' + str2). Using strings with U+FFFE may yield shorter sort keys.
For details about Sort Key Features see https://unicode-org.github.io/icu/userguide/collation/api#sort-key-features
It is possible to merge multiple sort keys by consecutively merging another one with the intermediate result.
Only the sort key bytes of the CollationKeys are merged. This API does not attempt to merge the String representations of the CollationKeys, hence null will be returned as the result's String representation.
Example (uncompressed):
191B1D 01 050505 01 910505 00 1F2123 01 050505 01 910505 00will be merged as
191B1D 02 1F2123 01 050505 02 050505 01 910505 02 910505 00
source
- CollationKey to merge withIllegalArgumentException
- thrown if source CollationKey
argument is null or of 0 length.Copyright © 2016 Unicode, Inc. and others.