当前位置 类层次(JDK) 所有类 (JDK) 所有域和方法 (JDK)

类 java.text.CollationKey

java.lang.Object
   |
   +----java.text.CollationKey

public final class CollationKey
extends Object

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() );
 

参见:
Collator, RuleBasedCollator

方法索引

compareTo(CollationKey)
把该 CollationKey 与目标 CollationKey 比较。
equals(Object)
比较该 CollationKey 与目标 CollationKey 是否相等。
getSourceString()
返回 CollationKey 描述的 String 。
hashCode()
为该 CollationKey 创建一个散列码。
toByteArray()
把 CollationKey 转换成位序列。

方法

compareTo
 public int compareTo(CollationKey target)
把该 CollationKey 与目标 CollationKey 比较。 创建这些关键字的 Collator 对象的整理规则被应用。 注意: 由不同的 Collator 创建的 CollationKeys 不能进行比较。

参数:
target - 目标 CollationKey
返回值:
返回一个整数值。如果相等返回值小于零,如果大于则返回大于零的值,否则返回小于零的值。
参见:
compare
equals
 public boolean equals(Object target)
比较该 CollationKey 与目标 CollationKey 是否相等。 应用创建这些关键字的 Collator 对象的整理规则。 注意: 由不同的 Collator 创建的 CollationKeys 不能进行比较。

参数:
target - 目标 CollationKey 。
返回值:
如果两对象相等返回 true ,否则返回 false 。
覆盖:
Object 中的 equals
hashCode
 public int hashCode()
为该 CollationKey 创建一个散列码。 散列值对关键字本身计算,而不是创建关键字的 String 。这样,如果 x 和 y 是 CollationKeys, 则如果 x.equals(y) 为 true,x.hashCode(x) == y.hashCode() 。这允许在散列表中进行语言敏感的比较。参见 CollatinKey 类描述中的例子。

返回值:
基于字符串的整理顺序的散列值
覆盖:
Object 中的 hashCode
getSourceString
 public String getSourceString()
返回 CollationKey 描述的 String 。

toByteArray
 public byte[] toByteArray()
把 CollationKey 转换成位序列。 如果两个 CollationKeys 可以合理地比较,那么可以对每个关键字比较字节数组来获取同样的结果。字节数组对被组织的最重要的字节优先。


当前位置 类层次(JDK) 所有类 (JDK) 所有域和方法 (JDK)