我试图打印出一个哈希图,其中包含一个字符作为键和值作为另一个带有 Integer 和 Double 的哈希图
到目前为止我有这个,但没有用。
HashMap<Character, Map<Integer, Double>> MapInsideOfAMap = calc.MapInAMap(abc);
for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
System.out.println("Char: " + outer.getKey() + "\n");
for (Map.Entry<Character, Map<Integer, Double> inner : MapInsideOfAMap.getValue().entrySet()) {
System.out.println("int = " + inner.getKey() + ", double = " + inner.getValue());
}
}
最佳答案
你的代码应该是这样的
for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
System.out.println("Char: " + outer.getKey() + "\n");
for (Entry<Integer, Double> inner : MapInsideOfAMap.get(outer.getKey()).entrySet()) {
System.out.println("int = " + inner.getKey() + ", double = " + inner.getValue());
}
}
好的,我明白你想做什么,
既然已经有了Outer map entry,就不用再使用outer map reference了,直接这样就可以了,
for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
System.out.println("Char: " + outer.getKey() + "\n");
for (Entry<Integer, Double> inner : outer.getValue().entrySet()) {
System.out.println("int = " + inner.getKey() + ", double = " + inner.getValue());
}
}
关于java - 散列图中的散列图打印出来,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48643076/