在Java中访问深度嵌套的HashMap [英] Accessing Deeply nested HashMaps in Java

查看:75
本文介绍了在Java中访问深度嵌套的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个HashMap:

So I have this HashMap:

HashMap< String,HashMap< Float,HashMap< Float,String>>>

但是我不确定如何从最深层的嵌套结构中添加和删除元素.有人可以举个例子吗?

But I'm not to sure how to add and remove elements from the most deeply nest structure. Can someone give an example?

谢谢:)

更新:

感谢您的帮助,但是我如何才能将其放在HashMap的第一层呢?我尝试使用.put时遇到错误.

Thanks for the help, But how can I just put on the first level of the HashMap? I have tried .put and I am getting an error.

谢谢

推荐答案

首先创建地图:

HashMap<String, HashMap<Float,HashMap<Float, String>>> map = new HashMap<>();

然后将第一级地图放入其中:

Then put the first level map into it:

map.put("one", new HashMap<Float, HashMap<Float, String>>());

然后将HashMap放在最后一级:

Then put a HashMap in the last level:

map.get("one").put(1.0f,new HashMap<Float, String>());

现在在新地图中放置一个元素:

Now put an element in the new map:

map.get("one").get(1.0f).put(2.0f,"this is lame");

现在可以如上所述进行获取:

and now it can be gotten as described above:

System.out.println(map.get("one").get(1.0f).get(2.0f));

这篇关于在Java中访问深度嵌套的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆