THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

24.6. Internationalization and Localization for Text


The package java.text provides several types for localizing text behavior, such as collation (comparing
strings), and formatting and parsing text, numbers, and dates. You have already learned about dates in detail
so in this section we look at general formatting and parsing, and collation.


24.6.1. Collation


Comparing strings in a locale-sensitive fashion is called collation. The central class for collation is
Collator, which provides a compare method that takes two strings and returns an int less than, equal to,
or greater than zero as the first string is less than, equal to, or greater than the second.


As with most locale-sensitive classes, you get the best available Collator object for a locale from a
getInstance method, either passing a specific Locale object or specifying no locale and so using the
default locale. For example, you get the best available collator to sort a set of Russian-language strings like
this:


Locale russian = new Locale("ru", "");
Collator coll = Collator.getInstance(russian);


You then can use coll.compare to determine the order of strings. A Collator object takes localitynot
Unicode equivalenceinto account when comparing. For example, in a French-speaking locale, the characters ç
and c are considered equivalent for sorting purposes. A naïve sort that used String.compare would put
all strings starting with ç after all those starting with c (indeed, it would put them after z), but in a French
locale this would be wrong. They should be sorted according to the characters that follow the initial c or ç
characters in the strings.


Determining collation factors for a string can be expensive. A CollationKey object examines a string
once, so you can compare precomputed keys instead of comparing strings with a Collator. The method
Collator.getCollationKey returns a key for a string. For example, because Collator implements
the interface Comparator, you could use a Collator to maintain a sorted set of strings:


class CollatorSorting {
private TreeSet sortedStrings;


CollatorSorting(Collator collator) {


sortedStrings = new TreeSet(collator);
}


void add(String str) {
sortedStrings.add(str);
}


Iterator strings() {
return sortedStrings.iterator();
}
}


Each time a new string is inserted in sortedStrings, the Collator is used as a Comparator, with its
compare method invoked on various elements of the set until the TReeSet finds the proper place to insert
the string. This results in several comparisons. You can make this quicker at the cost of space by creating a

Free download pdf