本文整理匯總了Java中java.text.Collator.getCollationKey方法的典型用法代碼示例。如果您正苦於以下問題:Java Collator.getCollationKey方法的具體用法?Java Collator.getCollationKey怎麽用?Java Collator.getCollationKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.Collator
的用法示例。
在下文中一共展示了Collator.getCollationKey方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: TestCollationKey
import java.text.Collator; //導入方法依賴的package包/類
public final void TestCollationKey( )
{
logln("testing CollationKey begins...");
Collator col = null;
try {
col = Collator.getInstance(Locale.ROOT);
} catch (Exception foo) {
errln("Error : " + foo.getMessage());
errln("Default collation creation failed.");
}
if (col == null) {
return;
}
String test1 = "Abcda", test2 = "abcda";
logln("Use tertiary comparison level testing ....");
CollationKey sortk1 = col.getCollationKey(test1);
CollationKey sortk2 = col.getCollationKey(test2);
doAssert(sortk1.compareTo(sortk2) > 0,
"Result should be \"Abcda\" >>> \"abcda\"");
CollationKey sortk3 = sortk2;
CollationKey sortkNew = sortk1;
doAssert(sortk1 != sortk2, "The sort keys should be different");
doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");
doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");
doAssert(sortk1 == sortkNew, "The sort keys assignment failed");
doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");
doAssert(sortkNew != sortk3, "The sort keys should be different");
doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");
long cnt1, cnt2;
byte byteArray1[] = sortk1.toByteArray();
byte byteArray2[] = sortk2.toByteArray();
doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");
logln("testing sortkey ends...");
}
示例2: SortableArtist
import java.text.Collator; //導入方法依賴的package包/類
public SortableArtist(String name, String sortableName, Collator collator) {
this.name = name;
this.sortableName = sortableName;
collationKey = collator.getCollationKey(sortableName);
}
示例3: run
import java.text.Collator; //導入方法依賴的package包/類
public void run() {
/** debug: printout the test data
for (int i=0; i<sourceData_ja.length; i++){
System.out.println(i+": "+sourceData_ja[i]);
}
**/
/*
* 1. Test the backward compatibility
* note: targetData_ja.length is equal to sourceData_ja.length
*/
Collator myCollator = Collator.getInstance(Locale.JAPAN);
CollationKey[] keys = new CollationKey[sourceData_ja.length];
CollationKey[] target_keys = new CollationKey[targetData_ja.length];
for (int i=0; i<sourceData_ja.length; i++){
keys[i] = myCollator.getCollationKey(sourceData_ja[i]);
target_keys[i] = myCollator.getCollationKey(targetData_ja[i]); //used later
}
/* Sort the string using CollationKey */
InsertionSort(keys);
/** debug: printout the result after sort
System.out.println("--- After Sorting ---");
for (int i=0; i<sourceData_ja.length; i++){
System.out.println(i+" :"+keys[i].getSourceString());
}
**/
/*
* Compare the result using equals method and getSourceString method.
*/
boolean pass = true;
for (int i=0; i<sourceData_ja.length; i++){
/* Comparing using String.equals: in order to use getStringSource() */
if (! targetData_ja[i].equals(keys[i].getSourceString())){
throw new RuntimeException("FAILED: CollationKeyTest backward compatibility "
+"while comparing" +targetData_ja[i]+" vs "
+keys[i].getSourceString());
}
/* Comparing using CollaionKey.equals: in order to use equals() */
if (! target_keys[i].equals(keys[i])){
throw new RuntimeException("FAILED: CollationKeyTest backward compatibility."
+" Using CollationKey.equals " +targetData_ja[i]
+" vs " +keys[i].getSourceString());
}
/* Comparing using CollaionKey.hashCode(): in order to use hashCode() */
if (target_keys[i].hashCode() != keys[i].hashCode()){
throw new RuntimeException("FAILED: CollationKeyTest backward compatibility."
+" Using CollationKey.hashCode " +targetData_ja[i]
+" vs " +keys[i].getSourceString());
}
/* Comparing using CollaionKey.toByteArray(): in order to use toByteArray() */
byte[] target_bytes = target_keys[i].toByteArray();
byte[] source_bytes = keys[i].toByteArray();
for (int j=0; j<target_bytes.length; j++){
Byte targetByte = new Byte(target_bytes[j]);
Byte sourceByte = new Byte(source_bytes[j]);
if (targetByte.compareTo(sourceByte)!=0){
throw new RuntimeException("FAILED: CollationKeyTest backward "
+"compatibility. Using Byte.compareTo from CollationKey.toByteArray "
+targetData_ja[i]
+" vs " +keys[i].getSourceString());
}
}
}
testSubclassMethods();
testConstructor();
}