Printing a java map Map<String, Object>

This question already has an answer here:

  • How to efficiently iterate over each entry in a 'Map'? 38 answers

  • I'm sure there's some nice library that does this sort of thing already for you... But to just stick with the approach you're already going with, Map#entrySet gives you a combined Object with the key and the value . So something like:

    for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue().toString());
    }
    

    will do what you're after.


    If you're using java 8, there's also the new streaming approach.

    map.forEach((key, value) -> System.out.println(key + ":" + value));
    

    你可以使用Map.entrySet()方法:

    for (Map.Entry entry : objectSet.entrySet())
    {
        System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
    }
    

    在HashMap中有一个get方法:

    for (String keys : objectSet.keySet())  
    {
       System.out.println(keys + ":"+ objectSet.get(keys));
    }
    
    链接地址: http://www.djcxy.com/p/23966.html

    上一篇: 访问hashmap的值

    下一篇: 打印java映射Map <String,Object>