public abstract class CollationKey extends Object implements Comparable<CollationKey>
CollationKey表示String下一个特定的规则Collator对象。
比较两个CollationKey返回他们所代表的String的相对顺序。
使用CollationKey s比较String s通常比使用Collator.compare快。
因此,当String必须进行多次比较时,例如当排序String的列表时。
使用CollationKey更有效率。
您不能直接创建CollationKey 。 而是通过调用Collator.getCollationKey生成它们。 只能比较CollationKey来自同一生成S- Collator对象。
为CollationKey生成String涉及检查整个String并将其转换为可以按比例比较的一系列位。 一旦生成密钥,就可以快速比较。 在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());
Collator , RuleBasedCollator
| Modifier | Constructor and Description |
|---|---|
protected |
CollationKey(String source)
CollationKey构造函数。
|
| Modifier and Type | Method and Description |
|---|---|
abstract int |
compareTo(CollationKey target)
将此CollationKey与目标CollationKey进行比较。
|
String |
getSourceString()
返回此CollationKey表示的String。
|
abstract byte[] |
toByteArray()
将CollationKey转换为位序列。
|
protected CollationKey(String source)
source - 源字符串
NullPointerException - 如果
source为空
public abstract int compareTo(CollationKey target)
compareTo中的
Comparable<CollationKey>
target - 目标CollationKey
Collator.compare(java.lang.String, java.lang.String)
public String getSourceString()
public abstract byte[] toByteArray()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.