Nap
Nap

Reputation: 8276

Can a map use java.text.Collator?

Im using SQL Server 2008 R2.

I have a data cache retrieved from the database and currently the way to get a string is via map.get(). However, some parts of the code directly queries from the database instead of just using the cache since some joins are needed for those cases. The problem now, the collation on the table is causing different behavior between text comparison via SQL comparison (uses the table collation) and text comparison via Java using map.get().

I have read about java.text.Collator but it needs the use of collator.Compare to be able to use it properly. Is there a way to force a map in using a fixed Collator.

By the way the collation in this situation is using Japanese character where half-width and full-width Japanese are the same when compared in SQL Server.

Upvotes: 2

Views: 1417

Answers (2)

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15453

You can initiate Collator with Japanese Locale like:

Collator collator = Collator.getInstance( Locale.JAPANESE );

And then you can compare the strings like:

int comparison = collator.compare( strOne, strTwo );

Upvotes: 0

Emmanuel Bourg
Emmanuel Bourg

Reputation: 11058

You can use a TreeMap with a Collator properly configured for your case. Here is an example where uppercase and lowercase latin characters are considered equivalent:

Collator collator = Collator.getInstance(Locale.FRENCH);
collator.setStrength(Collator.PRIMARY);

Map<String, String> map = new TreeMap<String, String>(collator);

map.put("ABC", "foo");

System.out.println(map.get("abc"));

Upvotes: 5

Related Questions