Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class CollationKey

extends Object
implements Comparable<CollationKey>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable
Imports
com.ibm.icu.impl.coll.Collation

A CollationKey represents a String under the rules of a specific Collator object. Comparing two CollationKeys 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 Collators might differ. Hence comparing CollationKeys generated from different Collators 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 CollationKeys 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[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

Author
Syn Wee Quek
ICU Status
Stable since ICU 2.8.
See Also
Collator, RuleBasedCollator

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
CollationKey.BoundMode

Options that used in the API CollationKey.getBound() for getting a CollationKey based on the bound mode requested.

Field Summary

Modifier and TypeField and Description
private int
m_hashCode_

Hash code for the key

private byte[]
m_key_

Sequence of bytes that represents the sort key

private int
m_length_

Gets the length of this CollationKey

private String
m_source_

Source string this CollationKey represents

private static final int
MERGE_SEPERATOR_

Collation key merge seperator

Constructor Summary

AccessConstructor and Description
public
CollationKey(String
string this CollationKey is to represent
source
,
byte[]
array of bytes that represent the collation order of argument source terminated by a null
key
)

CollationKey constructor.

private
CollationKey(String source, byte[] key, int length)

Private constructor, takes a length argument so it need not be lazy-evaluated.

public
CollationKey(String
string this CollationKey is to represent
source
,
RawCollationKey
RawCollationKey object that represents the collation order of argument source.
key
)

CollationKey constructor that forces key to release its internal byte array for adoption.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

an integer value. If the value is less than zero this CollationKey is less than than target, if the value is zero they are equal, and if the value is greater than zero this CollationKey is greater than target.
compareTo
(CollationKey
target CollationKey
target
)

Implements java.lang.Comparable.compareTo.

Compare this CollationKey to another CollationKey.
public boolean

Returns:

true if the two keys compare as equal, false otherwise.
equals
(Object
the object to compare to.
target
)

Overrides java.lang.Object.equals.

Compare this CollationKey and the specified Object for equality.
public boolean

Returns:

true if two objects are equal, false otherwise.
equals
(CollationKey
the CollationKey to compare to.
target
)

Compare this CollationKey and the argument target CollationKey for equality.

public CollationKey

Returns:

the result bounded CollationKey with a valid sort order but a null String representation.
getBound
(int
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.
boundType
,
int
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.
noOfLevels
)

Produces a bound for the sort order of a given collation key and a strength level.

private int

Returns:

length of the CollationKey
getLength
()

Gets the length of the CollationKey

public String

Returns:

source string that this CollationKey represents
getSourceString
()

Return the source string that this CollationKey represents.

public int

Returns:

the hash value.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code for this CollationKey.
public CollationKey

Returns:

a CollationKey that contains the valid merged sort keys with a null String representation, i.e. new CollationKey(null, merged_sort_keys)
merge
(CollationKey
CollationKey to merge with
source
)

Merges this CollationKey with another.

public byte[]

Returns:

CollationKey value in a sequence of big-endian byte bytes terminated by a null.
toByteArray
()

Duplicates and returns the value of this CollationKey as a sequence of big-endian bytes terminated by a null.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

m_hashCode_back to summary
private int m_hashCode_

Hash code for the key

m_key_back to summary
private byte[] m_key_

Sequence of bytes that represents the sort key

m_length_back to summary
private int m_length_

Gets the length of this CollationKey

m_source_back to summary
private String m_source_

Source string this CollationKey represents

MERGE_SEPERATOR_back to summary
private static final int MERGE_SEPERATOR_

Collation key merge seperator

Constructor Detail

CollationKeyback to summary
public CollationKey(String source, byte[] key)

CollationKey constructor. This constructor is given public access, unlike the JDK version, to allow access to users extending the Collator class. See Collator#getCollationKey(String).

Parameters
source:String

string this CollationKey is to represent

key:byte[]

array of bytes that represent the collation order of argument source terminated by a null

ICU Status
Stable since ICU 2.8.
See Also
Collator
CollationKeyback to summary
private CollationKey(String source, byte[] key, int length)

Private constructor, takes a length argument so it need not be lazy-evaluated. There must be a 00 byte at key[length] and none before.

CollationKeyback to summary
public 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.

Parameters
source:String

string this CollationKey is to represent

key:RawCollationKey

RawCollationKey object that represents the collation order of argument source.

ICU Status
Stable since ICU 2.8.
See Also
Collator, RawCollationKey

Method Detail

compareToback to summary
public int compareTo(CollationKey target)

Implements java.lang.Comparable.compareTo.

Compare this CollationKey to another CollationKey. The collation rules of the Collator that created this key are applied.

Note

Comparison between CollationKeys created by different Collators might return incorrect results. See class documentation.

Parameters
target:CollationKey

target CollationKey

Returns:int

an integer value. If the value is less than zero this CollationKey is less than than target, if the value is zero they are equal, and if the value is greater than zero this CollationKey is greater than target.

Annotations
@Override
Exceptions
NullPointerException:
is thrown if argument is null.
ICU Status
Stable since ICU 2.8.
See Also
Collator#compare(String, String)
equalsback to summary
public boolean equals(Object target)

Overrides java.lang.Object.equals.

Compare this CollationKey and the specified Object for equality. The collation rules of the Collator that created this key are applied.

See note in compareTo(CollationKey) for warnings about possible incorrect results.

Parameters
target:Object

the object to compare to.

Returns:boolean

true if the two keys compare as equal, false otherwise.

Annotations
@Override
Exceptions
ClassCastException:
is thrown when the argument is not a CollationKey. NullPointerException is thrown when the argument is null.
ICU Status
Stable since ICU 2.8.
See Also
compareTo(CollationKey)
equalsback to summary
public boolean equals(CollationKey target)

Compare this CollationKey and the argument target CollationKey for equality. The collation rules of the Collator object which created these objects are applied.

See note in compareTo(CollationKey) for warnings of incorrect results

Parameters
target:CollationKey

the CollationKey to compare to.

Returns:boolean

true if two objects are equal, false otherwise.

Exceptions
NullPointerException:
is thrown when the argument is null.
ICU Status
Stable since ICU 2.8.
getBoundback to summary
public CollationKey getBound(int boundType, int noOfLevels)

Produces a bound for the sort order of a given collation key and a strength level. This API does not attempt to find a bound for the CollationKey String representation, hence null will be returned in its place.

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.

Parameters
boundType:int

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:int

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.

Returns:CollationKey

the result bounded CollationKey with a valid sort order but a null String representation.

Exceptions
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.
ICU Status
Stable since ICU 2.6.
See Also
CollationKey, CollationKey.BoundMode, Collator#PRIMARY, Collator#SECONDARY, Collator#TERTIARY, Collator#QUATERNARY, Collator#IDENTICAL
getLengthback to summary
private int getLength()

Gets the length of the CollationKey

Returns:int

length of the CollationKey

getSourceStringback to summary
public String getSourceString()

Return the source string that this CollationKey represents.

Returns:String

source string that this CollationKey represents

ICU Status
Stable since ICU 2.8.
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code for this CollationKey. The hash value is calculated on the key itself, not the String from which the key was created. Thus if x and y are CollationKeys, then x.hashCode(x) == y.hashCode() if x.equals(y) is true. This allows language-sensitive comparison in a hash table.

Returns:int

the hash value.

Annotations
@Override
ICU Status
Stable since ICU 2.8.
mergeback to summary
public CollationKey merge(CollationKey source)

Merges this CollationKey with another. The levels are merged with their corresponding counterparts (primaries with primaries, secondaries with secondaries etc.). Between the values from the same level a separator is inserted.

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 00
will be merged as
191B1D 02 1F2123 01 050505 02 050505 01 910505 02 910505 00
Parameters
source:CollationKey

CollationKey to merge with

Returns:CollationKey

a CollationKey that contains the valid merged sort keys with a null String representation, i.e. new CollationKey(null, merged_sort_keys)

Exceptions
IllegalArgumentException:
thrown if source CollationKey argument is null or of 0 length.
ICU Status
Stable since ICU 2.6.
toByteArrayback to summary
public byte[] toByteArray()

Duplicates and returns the value of this CollationKey as a sequence of big-endian bytes terminated by a null.

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.");
Returns:byte[]

CollationKey value in a sequence of big-endian byte bytes terminated by a null.

ICU Status
Stable since ICU 2.8.
com.ibm.icu.text back to summary

public final Class CollationKey.BoundMode

extends Object
Class Inheritance

Options that used in the API CollationKey.getBound() for getting a CollationKey based on the bound mode requested.
ICU Status
Stable since ICU 2.6.

Field Summary

Modifier and TypeField and Description
public static final int
COUNT

One more than the highest normal BoundMode value.

public static final int
LOWER

Lower bound

public static final int
UPPER

Upper bound that will match strings of exact size

public static final int
UPPER_LONG

Upper bound that will match all the strings that have the same initial substring as the given string

Constructor Summary

AccessConstructor and Description
private
BoundMode()

Private Constructor

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COUNTback to summary
public static final int COUNT

Deprecated

ICU 58 The numeric value may change over time, see ICU ticket #12420.

One more than the highest normal BoundMode value.

Annotations
@Deprecated
LOWERback to summary
public static final int LOWER

Lower bound

ICU Status
Stable since ICU 2.6.
UPPERback to summary
public static final int UPPER

Upper bound that will match strings of exact size

ICU Status
Stable since ICU 2.6.
UPPER_LONGback to summary
public static final int UPPER_LONG

Upper bound that will match all the strings that have the same initial substring as the given string

ICU Status
Stable since ICU 2.6.

Constructor Detail

BoundModeback to summary
private BoundMode()

Private Constructor