java.text
Class Collator

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.text.Collator
All Implemented Interfaces:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Comparator sample code for java.util.Comparator definition code for java.util.Comparator <Object sample code for java.lang.Object definition code for java.lang.Object >
Direct Known Subclasses:
RuleBasedCollator sample code for java.text.RuleBasedCollator definition code for java.text.RuleBasedCollator

public abstract class Collator
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Comparator sample code for java.util.Comparator definition code for java.util.Comparator <Object sample code for java.lang.Object definition code for java.lang.Object >, Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

The Collator class performs locale-sensitive String comparison. You use this class to build searching and sorting routines for natural language text.

Collator is an abstract base class. Subclasses implement specific collation strategies. One subclass, RuleBasedCollator, is currently provided with the Java 2 platform and is applicable to a wide set of languages. Other subclasses may be created to handle more specialized needs.

Like other locale-sensitive classes, you can use the static factory method, getInstance, to obtain the appropriate Collator object for a given locale. You will only need to look at the subclasses of Collator if you need to understand the details of a particular collation strategy or if you need to modify that strategy.

The following example shows how to compare two strings using the Collator for the default locale.

 // Compare two strings in the default locale
 Collator myCollator = Collator.getInstance();
 if( myCollator.compare("abc", "ABC") < 0 )
     System.out.println("abc is less than ABC");
 else
     System.out.println("abc is greater than or equal to ABC");
 

You can set a Collator's strength property to determine the level of difference considered significant in comparisons. Four strengths are provided: PRIMARY, SECONDARY, TERTIARY, and IDENTICAL. The exact assignment of strengths to language features is locale dependant. For example, in Czech, "e" and "f" are considered primary differences, while "e" and "ê" are secondary differences, "e" and "E" are tertiary differences and "e" and "e" are identical. The following shows how both case and accents could be ignored for US English.

 //Get the Collator for US English and set its strength to PRIMARY
 Collator usCollator = Collator.getInstance(Locale.US);
 usCollator.setStrength(Collator.PRIMARY);
 if( usCollator.compare("abc", "ABC") == 0 ) {
     System.out.println("Strings are equivalent");
 }
 

For comparing Strings exactly once, the compare method provides the best performance. When sorting a list of Strings however, it is generally necessary to compare each String multiple times. In this case, CollationKeys provide better performance. The CollationKey class converts a String to a series of bits that can be compared bitwise against other CollationKeys. A CollationKey is created by a Collator object for a given String.
Note: CollationKeys from different Collators can not be compared. See the class description for CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey for an example using CollationKeys.

See Also:
RuleBasedCollator sample code for java.text.RuleBasedCollator definition code for java.text.RuleBasedCollator , CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey , CollationElementIterator sample code for java.text.CollationElementIterator definition code for java.text.CollationElementIterator , Locale sample code for java.util.Locale definition code for java.util.Locale

Field Summary
static int CANONICAL_DECOMPOSITION sample code for java.text.Collator.CANONICAL_DECOMPOSITION definition code for java.text.Collator.CANONICAL_DECOMPOSITION
          Decomposition mode value.
static int FULL_DECOMPOSITION sample code for java.text.Collator.FULL_DECOMPOSITION definition code for java.text.Collator.FULL_DECOMPOSITION
          Decomposition mode value.
static int IDENTICAL sample code for java.text.Collator.IDENTICAL definition code for java.text.Collator.IDENTICAL
          Collator strength value.
static int NO_DECOMPOSITION sample code for java.text.Collator.NO_DECOMPOSITION definition code for java.text.Collator.NO_DECOMPOSITION
          Decomposition mode value.
static int PRIMARY sample code for java.text.Collator.PRIMARY definition code for java.text.Collator.PRIMARY
          Collator strength value.
static int SECONDARY sample code for java.text.Collator.SECONDARY definition code for java.text.Collator.SECONDARY
          Collator strength value.
static int TERTIARY sample code for java.text.Collator.TERTIARY definition code for java.text.Collator.TERTIARY
          Collator strength value.
 
Constructor Summary
protected Collator sample code for java.text.Collator.Collator() definition code for java.text.Collator.Collator() ()
          Default constructor.
 
Method Summary
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.text.Collator.clone() definition code for java.text.Collator.clone() ()
          Overrides Cloneable
 int compare sample code for java.text.Collator.compare(java.lang.Object, java.lang.Object) definition code for java.text.Collator.compare(java.lang.Object, java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o1, Object sample code for java.lang.Object definition code for java.lang.Object  o2)
          Compares its two arguments for order.
abstract  int compare sample code for java.text.Collator.compare(java.lang.String, java.lang.String) definition code for java.text.Collator.compare(java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  source, String sample code for java.lang.String definition code for java.lang.String  target)
          Compares the source string to the target string according to the collation rules for this Collator.
 boolean equals sample code for java.text.Collator.equals(java.lang.Object) definition code for java.text.Collator.equals(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  that)
          Compares the equality of two Collators.
 boolean equals sample code for java.text.Collator.equals(java.lang.String, java.lang.String) definition code for java.text.Collator.equals(java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  source, String sample code for java.lang.String definition code for java.lang.String  target)
          Convenience method for comparing the equality of two strings based on this Collator's collation rules.
static Locale sample code for java.util.Locale definition code for java.util.Locale [] getAvailableLocales sample code for java.text.Collator.getAvailableLocales() definition code for java.text.Collator.getAvailableLocales() ()
          Returns an array of all locales for which the getInstance methods of this class can return localized instances.
abstract  CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey getCollationKey sample code for java.text.Collator.getCollationKey(java.lang.String) definition code for java.text.Collator.getCollationKey(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  source)
          Transforms the String into a series of bits that can be compared bitwise to other CollationKeys.
 int getDecomposition sample code for java.text.Collator.getDecomposition() definition code for java.text.Collator.getDecomposition() ()
          Get the decomposition mode of this Collator.
static Collator sample code for java.text.Collator definition code for java.text.Collator getInstance sample code for java.text.Collator.getInstance() definition code for java.text.Collator.getInstance() ()
          Gets the Collator for the current default locale.
static Collator sample code for java.text.Collator definition code for java.text.Collator getInstance sample code for java.text.Collator.getInstance(java.util.Locale) definition code for java.text.Collator.getInstance(java.util.Locale) (Locale sample code for java.util.Locale definition code for java.util.Locale  desiredLocale)
          Gets the Collator for the desired locale.
 int getStrength sample code for java.text.Collator.getStrength() definition code for java.text.Collator.getStrength() ()
          Returns this Collator's strength property.
abstract  int hashCode sample code for java.text.Collator.hashCode() definition code for java.text.Collator.hashCode() ()
          Generates the hash code for this Collator.
 void setDecomposition sample code for java.text.Collator.setDecomposition(int) definition code for java.text.Collator.setDecomposition(int) (int decompositionMode)
          Set the decomposition mode of this Collator.
 void setStrength sample code for java.text.Collator.setStrength(int) definition code for java.text.Collator.setStrength(int) (int newStrength)
          Sets this Collator's strength property.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

PRIMARY sample code for java.text.Collator.PRIMARY

public static final int PRIMARY
Collator strength value. When set, only PRIMARY differences are considered significant during comparison. The assignment of strengths to language features is locale dependant. A common example is for different base letters ("a" vs "b") to be considered a PRIMARY difference.

See Also:
setStrength(int) sample code for java.text.Collator.setStrength(int) definition code for java.text.Collator.setStrength(int) , getStrength() sample code for java.text.Collator.getStrength() definition code for java.text.Collator.getStrength() , Constant Field Values

SECONDARY sample code for java.text.Collator.SECONDARY

public static final int SECONDARY
Collator strength value. When set, only SECONDARY and above differences are considered significant during comparison. The assignment of strengths to language features is locale dependant. A common example is for different accented forms of the same base letter ("a" vs "ä") to be considered a SECONDARY difference.

See Also:
setStrength(int) sample code for java.text.Collator.setStrength(int) definition code for java.text.Collator.setStrength(int) , getStrength() sample code for java.text.Collator.getStrength() definition code for java.text.Collator.getStrength() , Constant Field Values

TERTIARY sample code for java.text.Collator.TERTIARY

public static final int TERTIARY
Collator strength value. When set, only TERTIARY and above differences are considered significant during comparison. The assignment of strengths to language features is locale dependant. A common example is for case differences ("a" vs "A") to be considered a TERTIARY difference.

See Also:
setStrength(int) sample code for java.text.Collator.setStrength(int) definition code for java.text.Collator.setStrength(int) , getStrength() sample code for java.text.Collator.getStrength() definition code for java.text.Collator.getStrength() , Constant Field Values

IDENTICAL sample code for java.text.Collator.IDENTICAL

public static final int IDENTICAL
Collator strength value. When set, all differences are considered significant during comparison. The assignment of strengths to language features is locale dependant. A common example is for control characters ("\u0001" vs "\u0002") to be considered equal at the PRIMARY, SECONDARY, and TERTIARY levels but different at the IDENTICAL level. Additionally, differences between pre-composed accents such as "\u00C0" (A-grave) and combining accents such as "A\u0300" (A, combining-grave) will be considered significant at the IDENTICAL level if decomposition is set to NO_DECOMPOSITION.

See Also:
Constant Field Values

NO_DECOMPOSITION sample code for java.text.Collator.NO_DECOMPOSITION

public static final int NO_DECOMPOSITION
Decomposition mode value. With NO_DECOMPOSITION set, accented characters will not be decomposed for collation. This is the default setting and provides the fastest collation but will only produce correct results for languages that do not use accents.

See Also:
getDecomposition() sample code for java.text.Collator.getDecomposition() definition code for java.text.Collator.getDecomposition() , setDecomposition(int) sample code for java.text.Collator.setDecomposition(int) definition code for java.text.Collator.setDecomposition(int) , Constant Field Values

CANONICAL_DECOMPOSITION sample code for java.text.Collator.CANONICAL_DECOMPOSITION

public static final int CANONICAL_DECOMPOSITION
Decomposition mode value. With CANONICAL_DECOMPOSITION set, characters that are canonical variants according to Unicode standard will be decomposed for collation. This should be used to get correct collation of accented characters.

CANONICAL_DECOMPOSITION corresponds to Normalization Form D as described in Unicode Technical Report #15.

See Also:
getDecomposition() sample code for java.text.Collator.getDecomposition() definition code for java.text.Collator.getDecomposition() , setDecomposition(int) sample code for java.text.Collator.setDecomposition(int) definition code for java.text.Collator.setDecomposition(int) , Constant Field Values

FULL_DECOMPOSITION sample code for java.text.Collator.FULL_DECOMPOSITION

public static final int FULL_DECOMPOSITION
Decomposition mode value. With FULL_DECOMPOSITION set, both Unicode canonical variants and Unicode compatibility variants will be decomposed for collation. This causes not only accented characters to be collated, but also characters that have special formats to be collated with their norminal form. For example, the half-width and full-width ASCII and Katakana characters are then collated together. FULL_DECOMPOSITION is the most complete and therefore the slowest decomposition mode.

FULL_DECOMPOSITION corresponds to Normalization Form KD as described in Unicode Technical Report #15.

See Also:
getDecomposition() sample code for java.text.Collator.getDecomposition() definition code for java.text.Collator.getDecomposition() , setDecomposition(int) sample code for java.text.Collator.setDecomposition(int) definition code for java.text.Collator.setDecomposition(int) , Constant Field Values
Constructor Detail

Collator sample code for java.text.Collator() definition code for java.text.Collator()

protected Collator()
Default constructor. This constructor is protected so subclasses can get access to it. Users typically create a Collator sub-class by calling the factory method getInstance.

See Also:
getInstance() sample code for java.text.Collator.getInstance() definition code for java.text.Collator.getInstance()
Method Detail

getInstance sample code for java.text.Collator.getInstance() definition code for java.text.Collator.getInstance()

public static Collator sample code for java.text.Collator definition code for java.text.Collator  getInstance()
Gets the Collator for the current default locale. The default locale is determined by java.util.Locale.getDefault.

Returns:
the Collator for the default locale.(for example, en_US)
See Also:
Locale.getDefault() sample code for java.util.Locale.getDefault() definition code for java.util.Locale.getDefault()

getInstance sample code for java.text.Collator.getInstance(java.util.Locale) definition code for java.text.Collator.getInstance(java.util.Locale)

public static Collator sample code for java.text.Collator definition code for java.text.Collator  getInstance(Locale sample code for java.util.Locale definition code for java.util.Locale  desiredLocale)
Gets the Collator for the desired locale.

Parameters:
desiredLocale - the desired locale.
Returns:
the Collator for the desired locale.
See Also:
Locale sample code for java.util.Locale definition code for java.util.Locale , ResourceBundle sample code for java.util.ResourceBundle definition code for java.util.ResourceBundle

compare sample code for java.text.Collator.compare(java.lang.String, java.lang.String) definition code for java.text.Collator.compare(java.lang.String, java.lang.String)

public abstract int compare(String sample code for java.lang.String definition code for java.lang.String  source,
                            String sample code for java.lang.String definition code for java.lang.String  target)
Compares the source string to the target string according to the collation rules for this Collator. Returns an integer less than, equal to or greater than zero depending on whether the source String is less than, equal to or greater than the target string. See the Collator class description for an example of use.

For a one time comparison, this method has the best performance. If a given String will be involved in multiple comparisons, CollationKey.compareTo has the best performance. See the Collator class description for an example using CollationKeys.

Parameters:
source - the source string.
target - the target string.
Returns:
Returns an integer value. Value is less than zero if source is less than target, value is zero if source and target are equal, value is greater than zero if source is greater than target.
See Also:
CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey , getCollationKey(java.lang.String) sample code for java.text.Collator.getCollationKey(java.lang.String) definition code for java.text.Collator.getCollationKey(java.lang.String)

compare sample code for java.text.Collator.compare(java.lang.Object, java.lang.Object) definition code for java.text.Collator.compare(java.lang.Object, java.lang.Object)

public int compare(Object sample code for java.lang.Object definition code for java.lang.Object  o1,
                   Object sample code for java.lang.Object definition code for java.lang.Object  o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

This implementation merely returns compare((String)o1, (String)o2) .

Specified by:
compare sample code for java.util.Comparator.compare(T, T) definition code for java.util.Comparator.compare(T, T) in interface Comparator sample code for java.util.Comparator definition code for java.util.Comparator <Object sample code for java.lang.Object definition code for java.lang.Object >
Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - the arguments cannot be cast to Strings.
Since:
1.2
See Also:
Comparator sample code for java.util.Comparator definition code for java.util.Comparator

getCollationKey sample code for java.text.Collator.getCollationKey(java.lang.String) definition code for java.text.Collator.getCollationKey(java.lang.String)

public abstract CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey  getCollationKey(String sample code for java.lang.String definition code for java.lang.String  source)
Transforms the String into a series of bits that can be compared bitwise to other CollationKeys. CollationKeys provide better performance than Collator.compare when Strings are involved in multiple comparisons. See the Collator class description for an example using CollationKeys.

Parameters:
source - the string to be transformed into a collation key.
Returns:
the CollationKey for the given String based on this Collator's collation rules. If the source String is null, a null CollationKey is returned.
See Also:
CollationKey sample code for java.text.CollationKey definition code for java.text.CollationKey , compare(java.lang.String, java.lang.String) sample code for java.text.Collator.compare(java.lang.String, java.lang.String) definition code for java.text.Collator.compare(java.lang.String, java.lang.String)

equals sample code for java.text.Collator.equals(java.lang.String, java.lang.String) definition code for java.text.Collator.equals(java.lang.String, java.lang.String)

public boolean equals(String sample code for java.lang.String definition code for java.lang.String  source,
                      String sample code for java.lang.String definition code for java.lang.String  target)
Convenience method for comparing the equality of two strings based on this Collator's collation rules.

Parameters:
source - the source string to be compared with.
target - the target string to be compared with.
Returns:
true if the strings are equal according to the collation rules. false, otherwise.
See Also:
compare(java.lang.String, java.lang.String) sample code for java.text.Collator.compare(java.lang.String, java.lang.String) definition code for java.text.Collator.compare(java.lang.String, java.lang.String)

getStrength sample code for java.text.Collator.getStrength() definition code for java.text.Collator.getStrength()

public int getStrength()
Returns this Collator's strength property. The strength property determines the minimum level of difference considered significant during comparison. See the Collator class description for an example of use.

Returns:
this Collator's current strength property.
See Also:
setStrength(int) sample code for java.text.Collator.setStrength(int) definition code for java.text.Collator.setStrength(int) , PRIMARY sample code for java.text.Collator.PRIMARY definition code for java.text.Collator.PRIMARY , SECONDARY sample code for java.text.Collator.SECONDARY definition code for java.text.Collator.SECONDARY , TERTIARY sample code for java.text.Collator.TERTIARY definition code for java.text.Collator.TERTIARY , IDENTICAL sample code for java.text.Collator.IDENTICAL definition code for java.text.Collator.IDENTICAL

setStrength sample code for java.text.Collator.setStrength(int) definition code for java.text.Collator.setStrength(int)

public void setStrength(int newStrength)
Sets this Collator's strength property. The strength property determines the minimum level of difference considered significant during comparison. See the Collator class description for an example of use.

Parameters:
newStrength - the new strength value.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the new strength value is not one of PRIMARY, SECONDARY, TERTIARY or IDENTICAL.
See Also:
getStrength() sample code for java.text.Collator.getStrength() definition code for java.text.Collator.getStrength() , PRIMARY sample code for java.text.Collator.PRIMARY definition code for java.text.Collator.PRIMARY , SECONDARY sample code for java.text.Collator.SECONDARY definition code for java.text.Collator.SECONDARY , TERTIARY sample code for java.text.Collator.TERTIARY definition code for java.text.Collator.TERTIARY , IDENTICAL sample code for java.text.Collator.IDENTICAL definition code for java.text.Collator.IDENTICAL

getDecomposition sample code for java.text.Collator.getDecomposition() definition code for java.text.Collator.getDecomposition()

public int getDecomposition()
Get the decomposition mode of this Collator. Decomposition mode determines how Unicode composed characters are handled. Adjusting decomposition mode allows the user to select between faster and more complete collation behavior.

The three values for decomposition mode are:

See the documentation for these three constants for a description of their meaning.

Returns:
the decomposition mode
See Also:
setDecomposition(int) sample code for java.text.Collator.setDecomposition(int) definition code for java.text.Collator.setDecomposition(int) , NO_DECOMPOSITION sample code for java.text.Collator.NO_DECOMPOSITION definition code for java.text.Collator.NO_DECOMPOSITION , CANONICAL_DECOMPOSITION sample code for java.text.Collator.CANONICAL_DECOMPOSITION definition code for java.text.Collator.CANONICAL_DECOMPOSITION , FULL_DECOMPOSITION sample code for java.text.Collator.FULL_DECOMPOSITION definition code for java.text.Collator.FULL_DECOMPOSITION

setDecomposition sample code for java.text.Collator.setDecomposition(int) definition code for java.text.Collator.setDecomposition(int)

public void setDecomposition(int decompositionMode)
Set the decomposition mode of this Collator. See getDecomposition for a description of decomposition mode.

Parameters:
decompositionMode - the new decomposition mode.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the given value is not a valid decomposition mode.
See Also:
getDecomposition() sample code for java.text.Collator.getDecomposition() definition code for java.text.Collator.getDecomposition() , NO_DECOMPOSITION sample code for java.text.Collator.NO_DECOMPOSITION definition code for java.text.Collator.NO_DECOMPOSITION , CANONICAL_DECOMPOSITION sample code for java.text.Collator.CANONICAL_DECOMPOSITION definition code for java.text.Collator.CANONICAL_DECOMPOSITION , FULL_DECOMPOSITION sample code for java.text.Collator.FULL_DECOMPOSITION definition code for java.text.Collator.FULL_DECOMPOSITION

getAvailableLocales sample code for java.text.Collator.getAvailableLocales() definition code for java.text.Collator.getAvailableLocales()

public static Locale sample code for java.util.Locale definition code for java.util.Locale [] getAvailableLocales()
Returns an array of all locales for which the getInstance methods of this class can return localized instances. The array returned must contain at least a Locale instance equal to Locale.US sample code for java.util.Locale.US definition code for java.util.Locale.US .

Returns:
An array of locales for which localized Collator instances are available.

clone sample code for java.text.Collator.clone() definition code for java.text.Collator.clone()

public Object sample code for java.lang.Object definition code for java.lang.Object  clone()
Overrides Cloneable

Overrides:
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a clone of this instance.
See Also:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

equals sample code for java.text.Collator.equals(java.lang.Object) definition code for java.text.Collator.equals(java.lang.Object)

public boolean equals(Object sample code for java.lang.Object definition code for java.lang.Object  that)
Compares the equality of two Collators.

Specified by:
equals sample code for java.util.Comparator.equals(java.lang.Object) definition code for java.util.Comparator.equals(java.lang.Object) in interface Comparator sample code for java.util.Comparator definition code for java.util.Comparator <Object sample code for java.lang.Object definition code for java.lang.Object >
Overrides:
equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) in class Object sample code for java.lang.Object definition code for java.lang.Object
Parameters:
that - the Collator to be compared with this.
Returns:
true if this Collator is the same as that Collator; false otherwise.
See Also:
Object.hashCode() sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable

hashCode sample code for java.text.Collator.hashCode() definition code for java.text.Collator.hashCode()

public abstract int hashCode()
Generates the hash code for this Collator.

Overrides:
hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object) sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable