Class CollationKey in Java

0
56
digitization, transformation, binary
Spread the love

Class CollationKey in Java

The CollationKey class optimizes the sorting of many strings. The easiest way to compare strings is by using Collator.compare(), 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.

Parameter
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 Throwable in Java

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

Parameter
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)


Spread the love
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here