How to put/get values into/from Nested HashMap

You have to get() the nested map out of the outer map and call can call put() and get() on it

float x = 1.0F;
HashMap<Float, Integer> innerMap = hashX.get(x);
if (innerMap == null) {
    hashX.put(x, innerMap = new HashMap<>()); // Java version >= 1.7
}
innerMap.put(2.0F, 5);

You can create a wrapper class with a method like this:

public class MyWrapper {
    private Map<Float, Map<Float, Integer>> hashX;
    // ...
    public void doublePut(Float one, Float two, Integer value) {
        if (hashX.get(one) == null) {
            hashX.put(one, new HashMap<Float, Integer>());
        }
      hashX.get(one).put(two, value);
    }
}

Please note that you should use interfaces instead of concrete implementations when you declare your fields. For example it would make easier to refactor HashMap into ConcurrentHashMap if the need arises.


Map<Float, Map<Float, Integer>> map = new HashMap<>();

map.put(.0F, new HashMap(){{put(.0F,0);}});
map.put(.1F, new HashMap(){{put(.1F,1);}});

map.get(.0F).get(.0F);