简体   繁体   English

向哈希表Java添加值

[英]Adding a value to a hashmap java

Sorry, new to Java, probably a really simple question. 抱歉,Java的新知识,可能是一个非常简单的问题。

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. 但是,我只想使用外面的地图添加MyVal3。

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. 在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 当您尝试获取不存在的密钥时要当心NullpointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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