Java Collection How to - Store value without considering case of String key in a Set








Question

We would like to know how to store value without considering case of String key in a Set.

Answer

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
//from  w  w w  .j av a  2s. c  o  m
public class Main {
    public static void main(String[] args) {
        Map<String, String> oldMap = new HashMap<String, String>();
        oldMap.put("a", "1");
        oldMap.put("A", "2");

        Map<String, String> newMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
        newMap.putAll(oldMap);
        String value = newMap.get("a");
        System.out.println(value);
    }

}

The code above generates the following result.