This
class compares, orders, and sorts strings in a way appropriate for
the default locale or some other specified locale. Because it is an
abstract class, it cannot be instantiated directly. Instead, you must
use the static getInstance( ) method to obtain an
instance of a Collator subclass that is
appropriate for the default or specified locale. You can use
getAvailableLocales( ) to determine whether a
Collator object is available for a desired locale.
Once an appropriate
Collator object has been obtained, you can use the
compare( ) method to compare strings. The possible
return values of this method are -1, 0, and 1, which indicate,
respectively, that the first string is collated before the second,
that the two are equivalent for collation purposes, and that the
first string is collated after the second. The equals(
) method is a convenient shortcut for testing two strings
for collation equivalence.
When sorting an array of strings, each string in the array is
typically compared more than once. Using the compare(
) method in this case is inefficient. A more efficient
method for comparing strings multiple times is to use
getCollationKey( )
for each string to create CollationKey objects.
These objects can then be compared to each other more quickly than
the strings themselves can be compared.
You can customize the way the
Collator object performs comparisons by calling
setStrength( ). If you pass the constant
PRIMARY to this method, the comparison looks only
at primary differences in the strings; it compares letters but
ignores accents and case differences. If you pass the constant
SECONDARY, it ignores case differences but does
not ignore accents. And if you pass TERTIARY (the
default), the Collator object takes both accents
and case differences into account in its comparison.
Figure 15-6. java.text.Collator
public abstract class
Collator implements java.util.Comparator<Object>,
Cloneable {
// Protected Constructors
protected
Collator ( );
// Public Constants
public static final int
CANONICAL_DECOMPOSITION ; =1
public static final int
FULL_DECOMPOSITION ; =2
public static final int
IDENTICAL ; =3
public static final int
NO_DECOMPOSITION ; =0
public static final int
PRIMARY ; =0
public static final int
SECONDARY ; =1
public static final int
TERTIARY ; =2
// Public Class Methods
public static java.util.Locale[ ]
getAvailableLocales ( ); synchronized
public static Collator
getInstance ( ); synchronized
public static Collator
getInstance (java.util.Locale
desiredLocale ); synchronized
// Public Instance Methods
public abstract int
compare (String
source , String
target );
public boolean
equals (Object
that ); Implements:Comparator
public boolean
equals (String
source , String
target );
public abstract CollationKey
getCollationKey (String
source );
public int
getDecomposition ( ); synchronized
public int
getStrength ( ); synchronized
public void
setDecomposition (int
decompositionMode ); synchronized
public void
setStrength (int
newStrength ); synchronized
// Methods Implementing Comparator
1.2 public int
compare (Object
o1 , Object
o2 );
public boolean
equals (Object
that );
// Public Methods Overriding Object
public Object
clone ( );
public abstract int
hashCode ( );
}