Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public abstract Class Collator

extends Object
implements Comparator<Object>, Freezable<Collator>, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, com.ibm.icu.util.Freezable, java.util.Comparator
Known Direct Subclasses
com.ibm.icu.text.RuleBasedCollator
Imports
java.util.Comparator, .LinkedList, .Locale, .MissingResourceException, .Set, com.ibm.icu.impl.ICUData, .ICUDebug, .ICUResourceBundle, .UResource, com.ibm.icu.impl.coll.CollationData, .CollationRoot, com.ibm.icu.lang.UCharacter, .UProperty, .UScript, com.ibm.icu.util.Freezable, .ICUException, .ULocale, .ULocale.Category, .UResourceBundle, .VersionInfo

ICU Enhancement

This class is ICU's replacement for java.text.Collator.

ICU

Methods, fields, and other functionality specific to ICU are labeled 'ICU'.

Collator performs locale-sensitive string comparison. A concrete subclass, RuleBasedCollator, allows customization of the collation ordering by the use of rule sets.

A Collator is thread-safe only when frozen. See isFrozen() and Freezable.

Following the Unicode Consortium's specifications for the Unicode Collation Algorithm (UCA), there are 5 different levels of strength used in comparisons:

Unlike the JDK, ICU4J's Collator deals only with 2 decomposition modes, the canonical decomposition mode and one that does not use any decomposition. The compatibility decomposition mode, java.text.Collator.FULL_DECOMPOSITION is not supported here. If the canonical decomposition mode is set, the Collator handles un-normalized text properly, producing the same results as if the text were normalized in NFD. If canonical decomposition is turned off, it is the user's responsibility to ensure that all text is already in the appropriate form before performing a comparison or before getting a CollationKey.

For more information about the collation service see the User Guide.

Examples of use

// 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");
}

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();
myCollator.setDecomposition(NO_DECOMPOSITION);
if (myCollator.compare("à\u0325", "a\u0325̀") != 0) {
    System.out.println("à\u0325 is not equals to a\u0325̀ without decomposition");
    myCollator.setDecomposition(CANONICAL_DECOMPOSITION);
    if (myCollator.compare("à\u0325", "a\u0325̀") != 0) {
        System.out.println("Error: à\u0325 should be equals to a\u0325̀ with decomposition");
    }
    else {
        System.out.println("à\u0325 is equals to a\u0325̀ with decomposition");
    }
}
else {
    System.out.println("Error: à\u0325 should be not equals to a\u0325̀ without decomposition");
}
Author
Syn Wee Quek
ICU Status
Stable since ICU 2.8.
See Also
RuleBasedCollator, CollationKey

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Collator.ASCII

Simpler/faster methods for ASCII than ones based on Unicode data.

public abstract static class
Collator.CollatorFactory

A factory used with registerFactory to register multiple collators and provide display names for them.

private static class
public static interface
Collator.ReorderCodes

Reordering codes for non-script groups that can be reordered under collation.

pack-priv abstract static class

Field Summary

Modifier and TypeField and Description
private static final String
BASE

The resource bundle base name for this service.

public static final int
CANONICAL_DECOMPOSITION

Decomposition mode value.

private static final boolean
public static final int
FULL_DECOMPOSITION

ICU Note

This is for backwards compatibility with Java APIs only.

public static final int
IDENTICAL

Smallest Collator strength value.

private static final String[]
KEYWORDS

The list of keywords for this service.

public static final int
NO_DECOMPOSITION

Decomposition mode value.

public static final int
PRIMARY

Strongest collator strength value.

public static final int
private static final String
RESOURCE

The resource name for this service.

public static final int
SECONDARY

Second level collator strength value.

private static Collator.ServiceShim
public static final int
TERTIARY

Third level collator strength value.

Constructor Summary

AccessConstructor and Description
protected
Collator()

Empty default constructor to make javadocs happy

Method Summary

Modifier and TypeMethod and Description
private void
public Object

Returns:

a clone of this collator.
clone
()

Overrides java.lang.Object.clone.

Clones the collator.
public Collator
cloneAsThawed()

Implements com.ibm.icu.util.Freezable.cloneAsThawed.

Provides for the clone operation.
public abstract int

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.
compare
(String
the source String.
source
,
String
the target String.
target
)

Redeclares java.util.Comparator.compare.

Compares the source text String to the target text String according to this Collator's rules, strength and decomposition mode.
public int

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.
compare
(Object
the source Object.
source
,
Object
the target Object.
target
)

Implements java.util.Comparator.compare.

Compares the source Object to the target Object.
protected int
doCompare(CharSequence left, CharSequence right)

Deprecated This API is ICU internal only.
Compares two CharSequences.
public boolean

Returns:

true if this Collator has exactly the same collation behavior as obj, false otherwise.
equals
(Object
the Collator to compare to.
obj
)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Compares the equality of two Collator objects.
public boolean

Returns:

true if the strings are equal according to the collation rules, otherwise false.
equals
(String
the source string to be compared.
source
,
String
the target string to be compared.
target
)

Compares the equality of two text Strings using this Collator's rules, strength and decomposition mode.

public Collator

Returns:

the collator itself.
freeze
()

Implements com.ibm.icu.util.Freezable.freeze.

Freezes the collator.
public static Locale[]

Returns:

the list of locales in which collators are installed. This list includes any that have been registered, in addition to those that are installed with ICU4J.
getAvailableLocales
()

Returns the set of locales, as Locale objects, for which collators are installed.

public static final ULocale[]

Returns:

the list of locales in which collators are installed. This list includes any that have been registered, in addition to those that are installed with ICU4J.
getAvailableULocales
()

ICU

public abstract CollationKey

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.
getCollationKey
(String
the string to be transformed into a CollationKey.
source
)

Transforms the String into a CollationKey suitable for efficient repeated comparison.

public int

Returns:

the decomposition mode
getDecomposition
()

Returns the decomposition mode of this Collator.

public static String

Returns:

the display name
getDisplayName
(Locale
the locale of the collator
objectLocale
,
Locale
the locale for the collator's display name
displayLocale
)

ICU

public static String

Returns:

the display name
getDisplayName
(ULocale
the locale of the collator
objectLocale
,
ULocale
the locale for the collator's display name
displayLocale
)

ICU

public static String

Returns:

the display name
getDisplayName
(Locale
the locale of the collator
objectLocale
)

ICU

public static String

Returns:

the display name
getDisplayName
(ULocale
the locale of the collator
objectLocale
)

ICU

public static int[]

Returns:

the set of all reorder codes in the same group as the given reorder code.
getEquivalentReorderCodes
(int
The reorder code to determine equivalence for.
reorderCode
)

Retrieves all the reorder codes that are grouped with the given reorder code.

public static final ULocale

Returns:

the locale
getFunctionalEquivalent
(String
a particular keyword as enumerated by getKeywords.
keyword
,
ULocale
The requested locale
locID
,
boolean[]
If non-null, isAvailable[0] will receive and output boolean that indicates whether the requested locale was 'available' to the collation service. If non-null, isAvailable must have length >= 1.
isAvailable
)

ICU

public static final ULocale

Returns:

the locale
getFunctionalEquivalent
(String
a particular keyword as enumerated by getKeywords.
keyword
,
ULocale
The requested locale
locID
)

ICU

public static final Collator

Returns:

the Collator for the default locale (for example, en_US) if it is created successfully. Otherwise if there is no Collator associated with the current locale, the root collator will be returned.
getInstance
()

Returns the Collator for the current default locale.

public static final Collator

Returns:

Collator for the desired locale if it is created successfully. Otherwise if there is no Collator associated with the current locale, the root collator will be returned.
getInstance
(ULocale
the desired locale.
locale
)

ICU

public static final Collator

Returns:

Collator for the desired locale if it is created successfully. Otherwise if there is no Collator associated with the current locale, the root collator will be returned.
getInstance
(Locale
the desired locale.
locale
)

Returns the Collator for the desired locale.

private static final int
getIntValue(String keyword, String s, String... values)

public static final String[]

Returns:

an array of valid collation keywords.
getKeywords
()

ICU

public static final String[]
getKeywordValues(String
one of the keywords returned by getKeywords.
keyword
)

ICU

public static final String[]

Returns:

an array of string values for the given key and the locale.
getKeywordValuesForLocale
(String
one of the keys supported by this service. For now, only "collation" is supported.
key
,
ULocale
the locale
locale
,
boolean
if set to true it will return only commonly used values with the given locale in preferred order. Otherwise, it will return all the available values for the locale.
commonlyUsed
)

ICU

public ULocale

Returns:

the information specified by type, or null if this object was not constructed from locale data.
getLocale
(ULocale.Type type)

ICU

public int

Returns:

the maximum variable reordering group.
getMaxVariable
()

ICU

public abstract RawCollationKey

Returns:

If key is null, a new instance of RawCollationKey will be created and returned, otherwise the user provided key will be returned.
getRawCollationKey
(String
the text String to be transformed into a RawCollationKey
source
,
RawCollationKey key)

ICU

private static final int
public int[]

Returns:

a copy of the reordering codes for this collator; if none are set then returns an empty array
getReorderCodes
()

Retrieves the reordering codes for this collator.

private static Collator.ServiceShim
public int

Returns:

this Collator's current strength attribute.
getStrength
()

Returns this Collator's strength attribute.

public UnicodeSet

Returns:

a pointer to a UnicodeSet object containing all the code points and sequences that may sort differently than in the root collator.
getTailoredSet
()

ICU

public abstract VersionInfo

Returns:

the version object associated with this collator
getUCAVersion
()

ICU

public abstract int

Returns:

the variable top primary weight
getVariableTop
()

ICU

public abstract VersionInfo

Returns:

the version object associated with this collator
getVersion
()

ICU

private static final boolean
getYesOrNo(String keyword, String s)

public int

Returns:

a hash code value.
hashCode
()

Overrides java.lang.Object.hashCode.

Generates a hash code for this Collator object.
public boolean
isFrozen()

Implements com.ibm.icu.util.Freezable.isFrozen.

Determines whether the object has been frozen or not.
public static final Object

Returns:

an object that can be used to unregister the registered factory.
registerFactory
(Collator.CollatorFactory
the factory to register
factory
)

ICU

public static final Object

Returns:

an object that can be used to unregister the registered collator.
registerInstance
(Collator
the collator to register
collator
,
ULocale
the locale for which this is the default collator
locale
)

ICU

private static void
setAttributesFromKeywords(ULocale loc, Collator coll, RuleBasedCollator rbc)

Sets collation attributes according to locale keywords.

public void
setDecomposition(int
the new decomposition mode
decomposition
)

Sets the decomposition mode of this Collator.

pack-priv void
setLocale(ULocale
the most specific locale containing any resource data, or null
valid
,
ULocale
the locale containing data used to construct this object, or null
actual
)

Set information about the locales that were used to create this object.

public Collator

Returns:

this
setMaxVariable
(int
one of Collator.ReorderCodes.SPACE, Collator.ReorderCodes.PUNCTUATION, Collator.ReorderCodes.SYMBOL, Collator.ReorderCodes.CURRENCY; or Collator.ReorderCodes.DEFAULT to restore the default max variable group
group
)

ICU

public void
setReorderCodes(int...
the reordering codes to apply to this collator; if this is null or an empty array then this clears any existing reordering
order
)

Sets the reordering codes for this collator.

public void
setStrength(int
the new strength value.
newStrength
)

Sets this Collator's strength attribute.

public Collator

Returns:

this, for chaining
setStrength2
(int newStrength)

Deprecated This API is ICU internal only.
public abstract int

Returns:

variable top primary weight
setVariableTop
(String
one or more (if contraction) characters to which the variable top should be set
varTop
)

Deprecated ICU 53 Call setMaxVariable(int) instead.

ICU

public abstract void
setVariableTop(int
primary weight, as returned by setVariableTop or getVariableTop
varTop
)

Deprecated ICU 53 Call setMaxVariable() instead.

ICU

public static final boolean

Returns:

true if the collator was successfully unregistered.
unregister
(Object
the object previously returned by registerInstance.
registryKey
)

ICU

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAlltoStringwaitwaitwait