How to put/get values into/from Nested HashMap(如何将值放入/从嵌套的 HashMap 中取出)
问题描述
我想创建一个嵌套的 HashMap,它将接受两个浮点类型的键并给出整数类型的值.
I want to create a nested HashMap that will take two keys of type float and give out value of type Integer.
public static HashMap<Float, HashMap<Float, Integer>> hashX = new HashMap<Float,HashMap<Float, Integer>>();
是否有一种简单的方法可以像普通的 HashMap 一样放置/获取值,即
Is there a simple method of putting/getting the values like an ordinary HashMap i.e.
hashX.put(key, value);
hashX.get(key);
还是必须使用更复杂的方法?我在网上搜索了解决方案,但发现很难找到适用于我的解决方案.任何帮助将不胜感激!
or is it a more complicated method that must be used? I have searched around the web for a solution but am finding it tough to find a solution that applies to me. Any help would be appreciated!
推荐答案
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);
这篇关于如何将值放入/从嵌套的 HashMap 中取出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!