How can I iterate over a map of ?

前端 未结 2 1197
夕颜
夕颜 2020-11-29 13:58

I\'ve got a Map (actually I\'m using a more complex POJO but simplifying it for the sake of my question)

Person looks

2条回答
  •  囚心锁ツ
    2020-11-29 14:33

    What about entrySet()

    HashMap hm = new HashMap();
    
    hm.put("A", new Person("p1"));
    hm.put("B", new Person("p2"));
    hm.put("C", new Person("p3"));
    hm.put("D", new Person("p4"));
    hm.put("E", new Person("p5"));
    
    Set> set = hm.entrySet();
    
    for (Map.Entry me : set) {
      System.out.println("Key :"+me.getKey() +" Name : "+ me.getValue().getName()+"Age :"+me.getValue().getAge());
    
    }
    

提交回复
热议问题