CKTIS / Java
Collator — comparing text using language rules
java.text.Collator compares text using the rules of a chosen Locale. It lets you configure which differences matter and how composed Unicode characters are handled.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Collator collator = Collator.getInstance(locale);
collator.setStrength(Collator.PRIMARY);
collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
int result = collator.compare(left, right);java.text.Collator is an abstract class for linguistic string comparison. Use getInstance(locale) rather than relying on the default Locale. The result of compare(left, right) is negative, zero, or positive; do not assume exactly -1 or 1. Zero means equivalence under the rules, not necessarily identical spelling.
setStrength selects distinguished levels: PRIMARY typically distinguishes base letters, SECONDARY additionally distinguishes accents, and TERTIARY also distinguishes case. The assignment of differences depends on the locale. IDENTICAL resolves remaining differences, but the result still depends on decomposition.
NO_DECOMPOSITION does not decompose characters; CANONICAL_DECOMPOSITION applies canonical decomposition, while FULL_DECOMPOSITION also applies compatibility decomposition. For RuleBasedCollator, canonical decomposition matters for combining characters in noncanonical order.
In the JDK implementation, a shared RuleBasedCollator may cause thread contention; consider a separate instance per thread. Entry scope: Java SE 21; the repository runtime and production runtime are not attested.
Examples
import java.text.Collator;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Collator collator = Collator.getInstance(Locale.US);
collator.setStrength(Collator.PRIMARY);
collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
System.out.println(collator.compare("abc", "ABC") == 0);
System.out.println(collator.compare("abc", "abd") < 0);
System.out.println(collator.compare("\u00E9", "e\u0301") == 0);
}
}Result
true true true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.