Class CollationKey in Java

Class CollationKey in Java

The CollationKey class optimizes the sorting of many strings. The easiest way to compare strings is by using, but this can be inefficient, especially if the same strings are compared many times. Instead, we can create a CollationKey for each of your strings and compare the CollationKey objects to each other using the compareTo() method.

A CollationKey is essentially a bit representation of a String object. Two CollationKey objects can be compared bitwise, which allows for a fast comparison.

We cannot create CollationKey objects directly; we must create them through a specific
Collator object using Collator.getCollationKey(). We can only compare CollationKey objects that have been generated from the same Collator.

The structure of the class CollationKey is given by

public final class java.text.CollationKey extends java.lang.Object {
// Instance Methods
public int compareTo(CollationKey target);
public boolean equals(Object target);
public String getSourceString();
public int hashCode();
public byte[] toByteArray();

The details of the class structure are given as follows:

public int compareTo(CollationKey target);

public int compareTo(CollationKey target) method returns an integer that indicates the ordering of this CollationKey and the given CollationKey. Only CollationKey objects generated by the same Collator should be compared.

This method returns -1 if this CollationKey is less than a target, 0 if this CollationKey is equal to target, or 1 if this CollationKey is greater than a target.

target – The key to compare with this CollationKey.

public boolean equals(Object target);

public boolean equals(Object target) method returns true if obj is an instance of CollationKey and is equivalent to this CollationKey.

READ  Class SecurityException in Java

This method returns true if the objects are equal; false if they are not.

target – The object to be compared with this object.

public String getSourceString();

public String getSourceString() method returns the string that was passed to Collator.getCollationKey() to create
this CollationKey.

This method returns The string that generated this CollationKey.

public int hashCode();

public int hashCode() method returns a hashcode for this CollationKey.
This method returns a hashcode for this object.

public byte[] toByteArray();

public byte[] toByteArray() method returns a byte array that represents the value of this CollationKey, with the most
significant byte first.

This method returns a byte array that represents this CollationKey.

Apart from these CollationKey class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *