打印出一个hasmap内的hashmap

时间:2018-02-06 12:26:41

标签: java collections hashmap

我正在尝试打印出一个包含A字符作为键的hashmap,并将该值作为另一个具有Integer和Double的hashmap打印出来 到目前为止我有这个但是没有用。

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());
        }
    }

7 个答案:

答案 0 :(得分:2)

让我们假设您的地图如下所示:

Map <Character, Map<Integer, Double>> MapInsideOfAMap = new HashMap();

然后你可以像这样打印你的地图:

for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
    System.out.println("Char: " + outer.getKey() + "\n");
    HashMap<Integer, Double> innermap = MapInsideOfAMap.get(outer.getKey());
    for (Map.Entry<Integer, Double> innerEntry : innermap.entrySet()) {
         System.out.println("int = " + innerEntry.getKey() + ", double = " + innerEntry.getValue());
    }
}

答案 1 :(得分:2)

您的代码应该是这样的,

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());
        }
    }

好的,我明白你要做什么, 因为你已经有了外部地图条目,你不必再次使用外部地图参考,你可以直接这样做,

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());
        }
    }

答案 2 :(得分:2)

如果您只需查看地图键/值,请使用System.out.println

地图AbstractMap.toString知道如何以漂亮可读的方式打印自己。

Map<Character, Map<Integer, Double>> map = new HashMap<>();
map.put('A', new HashMap<>());
map.get('A').put(1, 0.01);
map.put('B', new HashMap<>());
map.get('B').put(2, 0.02);
System.out.println(map);

打印出来:

{A={1=0.01}, B={2=0.02}}

答案 3 :(得分:0)

在第二个for循环中,您需要访问从外部循环获取的Map。 您还需要在第二个循环中更改Entry的类型。

试试这段代码:

for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
    System.out.println("Key: " + outer.getKey() + "\n");
    for (Entry<Integer, Double> inner : outer.getValue().entrySet()) {
        System.out.println("Key = " + inner.getKey() + ", Value = " + inner.getValue());
    }
}

答案 4 :(得分:0)

但结构的复杂性可能是多余的。

public static void print(char keyData, Map<Character, Map<Integer, Double>> fromData) {
    System.out.println("print: " + get(keyData, fromData));
}

public static Map<Integer, Double> get(char keyData, Map<Character, Map<Integer, Double>> fromData) {

    for (Map.Entry<Character, Map<Integer, Double>> entry : fromData.entrySet()) {

        Character key = entry.getKey();

        if(key.equals(keyData))
            return entry.getValue();

    }

    return Collections.emptyMap();
}

答案 5 :(得分:0)

简单地打印整个地图地图:

System.out.println(mapInsideOfAMap);

现在,如果要迭代外部和内部地图并打印其键/值对,可以使用Map.forEach方法:

mapInsideOfAMap.forEach((outerKey, outerValue) -> {
    System.out.println("Char: " + outerKey + "\n");
    outerValue.forEach((innerKey, innerValue) -> 
        System.out.println("int = " + innerKey + ", double = " + innerValue));
});

答案 6 :(得分:0)

public static void main(String z[]) {

    Map<Character, Map<Integer, Double>> MapInsideOfAMap = getmapOfMap();
    for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
        System.out.println("Char: " + outer.getKey() + "\n");

        Map<Integer, Double> mapInner = MapInsideOfAMap.get(outer.getKey());
        for (Map.Entry<Integer, Double> inner : mapInner.entrySet()) {
             System.out.println(inner.getKey() +":"+ mapInner.get(inner.getKey()));
        }
    }
}

private static Map getmapOfMap() {
    char[] chArr = {'a','b','c','d','e','f','g','h','i','j','k'};
    HashMap<Character, Map<Integer, Double>> mapInsideOfAMap = new HashMap<Character, Map<Integer, Double>>();

    for(char ch:chArr) {
        mapInsideOfAMap.put(ch, getInnterMap());

    }
    return mapInsideOfAMap;
}

private static Map getInnterMap() {
     Map<Integer, Double> map = new HashMap<>();

     for(int i=1000;i<1010;i++) {
         map.put(i, new Double(String.valueOf(i)));
     }
     return map;
}