I created CollationKey for a String "a" and then I used method toByteArray() to convert the CollationKey to a sequence of bits. After that I use Arrays.toString() to display this byte[] array and I get an output I don't understand. I thought I will get String represented in bits. How to interpret the output? Thank You
package myPackage9;
import java.text.CollationKey;
import java.text.*;
import java.lang.*;
import java.util.Arrays;
public class collatorClass {
public static void main(String[] args) {
Collator myCollator = Collator.getInstance();
CollationKey[] a = new CollationKey[1];
a[0] = myCollator.getCollationKey("a");
byte[] bytes= a[0].toByteArray();
System.out.println(Arrays.toString(bytes));
}
}
output: [0, 83, 0, 0, 0, 1, 0, 0, 0, 1]
CollationKeyis an abstract class. Most likely your concrete type is aRuleBasedCollationKey. First, let's look at the JavaDoc of the method:Apparently, the collation key of "a" is not represented by the same bytes as the string "a", which isn't all too surprising
The next step is to look at its source to understand what it is returning exactly:
What is
key? It is passed in as second constructor parameter. The constructor is called inRuleBasedCollator#getCollationKey. The source is quite complicated, but the method's JavaDoc states:Looking at the inline code comments of the method, it is explained further:
Followed by a hypothetical example:
So the assumption that a
CollationKey'stoByteArray()method would return the same as aString'stoByteArray()method is simply wrong."a".toByteArray()is not the same asCollator.getInstance().getCollationKey("a").toByteArray(). If it were, we wouldn't really need collation keys, would we?