简体   繁体   中英

Adding a value to a hashmap java

Sorry, new to Java, probably a really simple question.

Let's say I have an outter map, that has a (key, inner map), and in the inner map I have (String, Double).

So it looks something like this.

    HashMap<String, Double> inner = new HashMap<String, Double>();
    HashMap<Integer, Map<String, Double>> outter = new HashMap<Integer, Map<String, Double>>();
    inner.put("MyVal", 24.5930553450692151964475150);
    inner.put("MyVal2", 48.6514790522118734018261775);
    outter.put(20151205, inner)

I end up with and outter map like this:

{20151205={MyVal=24.593055345069214, MyVal2=48.651479052211876}}

Now let's say I no longer have access to the inner map, so I can't put any more values in it. But, I want to add a MyVal3 using only the outter map.

How can this be done?

I want to end up with something like this using code for only the outter map.

{20151205={MyVal=24.593055345069214, MyVal2=48.651479052211876, MyVal3=48.4846855555555}}

Thanks a lot! Couldn't find exactly this question elsewhere on SO.

As always thanks everyone!

You always have access to the inner map, but you need to get the key.

Then you simply put.

outter.get(20151205).put("MyVal3", 48.4846855555) ;

Beware of the Nullpointerexception when you try to get a key that doesn't exist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM