java.lang.Object | +----java.text.CollationKey
CollationKey 在特定的 Collator 对象的规则下描述了一个 String 。比较两个 CollationKey ,返回它们描述的 String 的相对顺序。使用 CollationKey 来比较 String 通常比使用 Collator.compare 要快。这样,当排序一个 String 表时,String 必须被比较多次。使用 CollationKey 就会更有效。
不能直接创建 CollationKey 。而是通过调用 Collator.getCollationKey 创建它们。只可以比较由同一 Collator 对象生成的
CollationKey 。
为一个 String 生成 CollationKey 涉及到检查整个 String 并把它转化为一系列可以进行位宽度比较的位。一旦关键字生成,则可实现比较快的比较。生成关键字的代价会因在 String 需要比较多次时比较加快而得到补偿。另一方面,比较结果经常由每个 String 的第一对字符决定。在每次单独的比较中,Collator.compare 仅检查需要检查的字符,这会使速度更快。
下面的例子演示了 CollationKey 如何用于排列
String 列表。
// 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() );
public int compareTo(CollationKey target)
public boolean equals(Object target)
public int hashCode()
public String getSourceString()
public byte[] toByteArray()