I needed recently to create a case-insensitive map in Java to query it with keys which might be uppercase or lowercase. I thought that it should be somewhere some kind of solution already available for developers. I found a class called CaseInsensitiveMap in Apache Commons Collections package (https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/CaseInsensitiveMap.html) but unfortunately, I found later on that it does not support generics which was needed in my case, that's why I decided to go with a custom solution for that problem which might be found below.
Map<String, String> record = getRecord(); TreeMap<String, String> caseInsensitiveMap = new TreeMap<>getCaseInsensitiveComparator()); caseInsensitiveMap.putAll(record); protected Comparator<String> getCaseInsensitiveComparator() { return Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER); }
Case Insensitive Map in Java
2018-07-11T22:43:00+01:00
GarciaPL
Java|Java 8|