I \'ve run into a scenario where I want to lowercase all the keys of a HashMap (don\'t ask why, I just have to do this). The HashMap has some millions of entries.
At fir
Instead of using HashMap
, you could try using a TreeMap
with case-insensitive ordering. This would avoid the need to create a lower-case version of each key:
Map map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
map.putAll(myMap);
Once you've constructed this map, put()
and get()
will behave case-insensitively, so you can save and fetch values using all-lowercase keys. Iterating over keys will return them in their original, possibly upper-case forms.
Here are some similar questions: