简体   繁体   English

我如何在地图上进行迭代 <String, POJO> ?

[英]How can I iterate over a map of <String, POJO>?

I've got a Map<String, Person> (actually I'm using a more complex POJO but simplifying it for the sake of my question) 我有一个Map<String, Person> (实际上,我在使用一个更复杂的POJO,但是为了我的问题而简化了它)

Person looks like : Person的样子:

class Person
{
  String name;
  Integer age;

  //accessors
}

How can I iterate through this map, printing out the key, then the person name, then the person age such as : 我如何遍历此地图,打印出密钥,然后打印人名,然后打印人年龄,例如:

System.out.println(String.format("Key : %s Name : %s Age : %s", a, b, c));
  • A being the key from Map< String , Person> A是Map < String ,Person>的键
  • B being the name from Person.getName() B是Person.getName()的名称
  • C being the age from Person.getAge() C是Person.getAge()的年龄

I can pull all of the values from the map using .values() as detailed in the HashMap docs , but I'm a bit unsure of how I can get the keys 我可以使用HashMap文档中详细介绍的.values()从地图中提取所有值,但是我不确定如何获取键

What about entrySet() 那么entrySet()

HashMap<String, Person> hm = new HashMap<String, Person>();

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<Map.Entry<String, Person>> set = hm.entrySet();

for (Map.Entry<String, Person> me : set) {
  System.out.println("Key :"+me.getKey() +" Name : "+ me.getValue().getName()+"Age :"+me.getValue().getAge());

}

You can use: 您可以使用:

Example: 例:

Map<String, Person> personMap = ..... //assuming it's not null
Iterator<String> strIter = personMap.keySet().iterator();
synchronized (strIter) {
    while (strIter.hasNext()) {
        String key = strIter.next();
        Person person = personMap.get(key);

        String a = key;
        String b = person.getName();
        String c = person.getAge().toString();
        System.out.println(String.format("Key : %s Name : %s Age : %s", a, b, c));

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历 Java 中的字符串? - How can I iterate over a string in Java? 如何迭代 inside.map() 中的字符串数组 - How to Iterate over a String Array inside .map() 如何使用ApacheD将POJO映射到目录? - How can i map a POJO to a directory with ApacheDs? 如何转换地图<String, String>使用 jackson 将 json 值转换为 POJO? - How can I convert a Map<String, String> with json values to a POJO using jackson? 迭代POJO属性 - Iterate over POJO properties 如何遍历地图 <String,String> 和地图 <String,Map<String,String> &gt;使用相同的递归函数? - How to iterate over Map<String,String> and Map<String,Map<String,String>> using the same recursion function? 如何使用 Java 8 遍历 Map 以获取重复字符键的字符串 - How to iterate over a Map to obtain a String of repeated Character Keys with Java 8 如何在另一个 map(比如 NewMap)上迭代地图(比如 OldMap)。 在 NewMap 中,我想将条目更新为 String,List<double> , 下面解释</double> - How to iterate map(say OldMap) over another map (say NewMap). In NewMap , I want to update the entries as String,List<Double> , Explained below 如何将结果集映射到 JPA 中的自定义 POJO - How can I map a result set to custom POJO in JPA 如何使用Morphia映射未注释的pojo? - How can I use Morphia to map a non-annotated pojo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM