[英]Accessing Deeply nested HashMaps in Java
So I have this HashMap: 所以我有这个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?
有人能举个例子吗?
Thanks :) 谢谢 :)
Update: 更新:
Thanks for the help, But how can I just put on the first level of the HashMap? 感谢您的帮助,但我怎样才能加入HashMap的第一级? I have tried .put and I am getting an error.
我试过.put,我收到了一个错误。
Thanks 谢谢
First create the map: 首先创建地图:
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>>());
Then put a HashMap in the last level: 然后将HashMap放在最后一级:
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));
If you plan on constructing homogeneous HashMaps with variable depth , use a recursive data structure . 如果您计划构建具有可变深度的同构HashMaps,请使用递归数据结构 。
Below is an implementation providing a sample interface: 以下是提供示例界面的实现:
class NestedMap<K, V> {
private final HashMap<K, NestedMap> child;
private V value;
public NestedMap() {
child = new HashMap<>();
value = null;
}
public boolean hasChild(K k) {
return this.child.containsKey(k);
}
public NestedMap<K, V> getChild(K k) {
return this.child.get(k);
}
public void makeChild(K k) {
this.child.put(k, new NestedMap());
}
public V getValue() {
return value;
}
public void setValue(V v) {
value = v;
}
}
and example usage: 和示例用法:
class NestedMapIllustration {
public static void main(String[] args) {
NestedMap<Character, String> m = new NestedMap<>();
m.makeChild('f');
m.getChild('f').makeChild('o');
m.getChild('f').getChild('o').makeChild('o');
m.getChild('f').getChild('o').getChild('o').setValue("bar");
System.out.println(
"nested element at 'f' -> 'o' -> 'o' is " +
m.getChild('f').getChild('o').getChild('o').getValue());
}
}
Having HashMap<String,HashMap<Float,HashMap<Float,String>>> map
and without accounting for null
values, just follow the logical sequence that you would formulate in your mind to access the inner map and translate to the following code: 让
HashMap<String,HashMap<Float,HashMap<Float,String>>> map
并且不考虑null
值,只需按照您在脑海中制定的逻辑顺序来访问内部地图并转换为以下代码:
map.get(strKey).get(floatKey).put(newFloat, newString);
map.get(strKey).get(floatKey).remove(newFloat);
strKey
is a key String
in the first-level map strKey
是第一级映射中的键String
floatKey
a key Float
in the second-level map floatKey
一键Float
在二级地图中
Let's have a look, shall we? 我们来看看,好吗?
First layer is a HashMap<String, HashMap>
, so let's get
. 第一层是
HashMap<String, HashMap>
,所以让我们get
。
map.get(strKey); // Returns another HashMap
We've got another HashMap
back, so what do we do? 我们还有另一个
HashMap
,那么我们该怎么办? We use get
again! 我们使用
get
了!
map.get(strKey).get(1.0f); // Returns another HashMap
Again, what do we do? 再说一次,我们该怎么办? Well only one thing for it.
那只有一件事。
get
! get
!
map.get(strKey).get(1.0f).get(1.0f); // Returns a String
This is the value in the deeply nested HashMap
. 这是深度嵌套的
HashMap
的值。
So first you will do a get on the first HashMap with a String as key. 首先,您将使用String作为键来获取第一个HashMap。 It will return you an
HashMap<Float,HashMap<Float,String>>
. 它将返回一个
HashMap<Float,HashMap<Float,String>>
。
You will then do a get on that HashMap with a Float as key. 然后,您将使用Float作为键来获取该HashMap。 It will return a
HashMap<Float,String>
. 它将返回
HashMap<Float,String>
。
You will finally do a get on that HashMap with a Float as key and there, you have the String you want. 你将最终用Float作为键来获取HashMap,在那里,你有你想要的String。 Same thing with a put instead of get on the last HashMap to insert a value.
与put相同而不是在最后一个HashMap上插入一个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.