打印包含String和对象列表的HashMap

时间:2015-05-14 18:21:11

标签: java hashmap

如何打印包含String和对象列表的HashMap?

HashMap<String, List<Object>> Map = new HashMap<String, List<Object>>();

每个对象都有一个名称和一个键值,所以我需要得到类似的东西:

StringFirst = [ firstObject 15, second object 31, thirdobject 16]
StringSecond = [ fourthObject 15, fifthObject 31, sixthObject 16]

    public static class Graph {

    public class Edge extends Graph {
        String edgeName;
        int time;

        Edge(String edgeName, int time){
            this.edgeName = edgeName;
            this.time = time;
        }

        public void Out(){
            System.out.println(this.edgeName);
            System.out.println(this.time);
        }
    }

2 个答案:

答案 0 :(得分:4)

尝试打印地图:

for(Map.Entry<String, ArrayList<Edge>> e : map.entrySet()){
   for(Edge e1 : e.getValue())
      System.out.println(e.getKey() + " = "+ e1.Out());
}

答案 1 :(得分:0)

迭代地图的最简单方法是使用键集。

shutdown.py

for(String key: map.keyset()) { System.out.println(key + " = " + map.get(key)); }上,您可能需要以不同方式处理它,或者遍历返回的列表

或者,这在功能上是相同的,

.get(key)