|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.ibm.icu.text.CollationKey
A 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 characterictics
of these two approaches can differ.
During the construction of a CollationKey
, the
entire source string is examined and processed into a series of
bits 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() );
Collator
,
RuleBasedCollator
Constructor Summary | |
CollationKey(java.lang.String source,
byte[] key)
CollationKey constructor. |
Method Summary | |
int |
compareTo(CollationKey target)
Compare this CollationKey to another CollationKey. |
int |
compareTo(java.lang.Object obj)
Compare this CollationKey with the specified Object. |
boolean |
equals(CollationKey target)
Compare this CollationKey and the argument target CollationKey for equality. |
boolean |
equals(java.lang.Object target)
Compare this CollationKey and the specified Object for equality. |
java.lang.String |
getSourceString()
Return the source string that this CollationKey represents. |
int |
hashCode()
Returns a hash code for this CollationKey. |
byte[] |
toByteArray()
Duplicates and returns the value of this CollationKey as a sequence of big-endian bytes terminated by a null. |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public CollationKey(java.lang.String source, byte[] key)
Collator.getCollationKey(String)
.source
- string this CollationKey is to representkey
- array of bytes that represent the collation order of argument
sourceCollator
Method Detail |
public java.lang.String getSourceString()
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.");
public int compareTo(CollationKey target)
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.
target
- target CollationKeyNullPointerException
- is thrown if argument is null.Collator.compare(String, String)
public int compareTo(java.lang.Object obj)
Compare this CollationKey with the specified Object. The collation rules of the Collator that created this key are applied.
See note in compareTo(CollationKey) for warnings about possible incorrect results.
compareTo
in interface java.lang.Comparable
obj
- the Object to be compared to.ClassCastException
- is thrown when the argument is not
a CollationKey. NullPointerException is thrown when the argument
is null.compareTo(CollationKey)
public boolean equals(java.lang.Object target)
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.
equals
in class java.lang.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)
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
target
- the CollationKey to compare to.NullPointerException
- is thrown when the argument is null.public int 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.
hashCode
in class java.lang.Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |