Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Collator (locale-sensitive) and compareTo (lexicographically) for comparing String values

Tags:

java

compareto

I've been reading about using Collator and the compareTo method in String for comparing Strings. I'm unsure what the real difference is between the two from reading the API. When is one to prefer over the other?

API Collator

API String compareTo

like image 958
Teletha Avatar asked Nov 27 '25 17:11

Teletha


1 Answers

Basically, locale-sensitive means that it takes into account the language being used and may use different weights for comparisons between different characters.

"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." 1

With the lexicographical comparison of compareTo it just uses their Unicode values instead of taking these different weights into account.

"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. "1

1 Colator Javadoc

like image 97
aoi222 Avatar answered Nov 30 '25 07:11

aoi222



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!