java.text
Class CollationKey

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.text.CollationKey
All Implemented Interfaces:
Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey >

public final class CollationKey
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey >

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. Using CollationKeys to compare Strings is generally faster than using Collator.compare. Thus, when the Strings must be compared multiple times, for example when sorting a list of Strings. It's more efficient to use CollationKeys.

You can not create CollationKeys directly. Rather, generate them by calling Collator.getCollationKey. You can only compare CollationKeys generated from the same Collator object.

Generating a CollationKey for a String involves examining the entire String and converting it to series of bits that can be compared bitwise. This allows fast comparisons once the keys are generated. The cost of generating keys is recouped in faster comparisons when Strings need to be compared many times. On the other hand, the result of a comparison is often determined by the first couple of characters of each String. Collator.compare examines only as many characters as it needs which allows it to be faster when doing single comparisons.

The following example shows how CollationKeys might 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() );

See Also:
Collator sample code for java.text.Collator definition code for java.text.Collator , RuleBasedCollator sample code for java.text.RuleBasedCollator definition code for java.text.RuleBasedCollator

Method Summary
 int compareTo sample code for java.text.CollationKey.compareTo(java.text.CollationKey) definition code for java.text.CollationKey.compareTo(java.text.CollationKey) (CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey  target)
          Compare this CollationKey to the target CollationKey.
 boolean equals sample code for java.text.CollationKey.equals(java.lang.Object) definition code for java.text.CollationKey.equals(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  target)
          Compare this CollationKey and the target CollationKey for equality.
 String sample code for java.lang.String definition code for java.lang.String getSourceString sample code for java.text.CollationKey.getSourceString() definition code for java.text.CollationKey.getSourceString() ()
          Returns the String that this CollationKey represents.
 int hashCode sample code for java.text.CollationKey.hashCode() definition code for java.text.CollationKey.hashCode() ()
          Creates a hash code for this CollationKey.
 byte[] toByteArray sample code for java.text.CollationKey.toByteArray() definition code for java.text.CollationKey.toByteArray() ()
          Converts the CollationKey to a sequence of bits.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Method Detail

compareTo sample code for java.text.CollationKey.compareTo(java.text.CollationKey) definition code for java.text.CollationKey.compareTo(java.text.CollationKey)

public int compareTo(CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey  target)
Compare this CollationKey to the target CollationKey. The collation rules of the Collator object which created these keys are applied. Note: CollationKeys created by different Collators can not be compared.

Specified by:
compareTo sample code for java.lang.Comparable.compareTo(T) definition code for java.lang.Comparable.compareTo(T) in interface Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey >
Parameters:
target - target CollationKey
Returns:
Returns an integer value. Value is less than zero if this is less than target, value is zero if this and target are equal and value is greater than zero if this is greater than target.
See Also:
Collator.compare(java.lang.String, java.lang.String) sample code for java.text.Collator.compare(java.lang.String, java.lang.String) definition code for java.text.Collator.compare(java.lang.String, java.lang.String)

equals sample code for java.text.CollationKey.equals(java.lang.Object) definition code for java.text.CollationKey.equals(java.lang.Object)

public boolean equals(Object sample code for java.lang.Object definition code for java.lang.Object  target)
Compare this CollationKey and the target CollationKey for equality. The collation rules of the Collator object which created these keys are applied. Note: CollationKeys created by different Collators can not be compared.

Overrides:
equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) in class Object sample code for java.lang.Object definition code for java.lang.Object
Parameters:
target - the CollationKey to compare to.
Returns:
Returns true if two objects are equal, false otherwise.
See Also:
Object.hashCode() sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable

hashCode sample code for java.text.CollationKey.hashCode() definition code for java.text.CollationKey.hashCode()

public int hashCode()
Creates 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. See the CollatinKey class description for an example.

Overrides:
hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
the hash value based on the string's collation order.
See Also:
Object.equals(java.lang.Object) sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable

getSourceString sample code for java.text.CollationKey.getSourceString() definition code for java.text.CollationKey.getSourceString()

public String sample code for java.lang.String definition code for java.lang.String  getSourceString()
Returns the String that this CollationKey represents.


toByteArray sample code for java.text.CollationKey.toByteArray() definition code for java.text.CollationKey.toByteArray()

public byte[] toByteArray()
Converts the CollationKey to a sequence of bits. If two CollationKeys could be legitimately compared, then one could compare the byte arrays for each of those keys to obtain the same result. Byte arrays are organized most significant byte first.