如何迭代 的映射?

发布于 2024-09-28 17:36:52 字数 729 浏览 0 评论 0 原文

我有一个 Map (实际上我使用的是更复杂的 POJO,但为了解决我的问题而简化了它)

Person 看起来像

class Person
{
  String name;
  Integer age;

  //accessors
}

:我可以迭代这个地图,打印出键,然后打印出人名,然后打印出人的年龄,例如:

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

我可以使用 .values() 从地图中提取所有值,如 HashMap docs,但我有点不确定如何获取密钥

I've got a Map<String, Person> (actually I'm using a more complex POJO but simplifying it for the sake of my question)

Person looks like :

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>
  • B being the name from Person.getName()
  • C being the age from 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-10-05 17:36:52

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

}

What about 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());

}
内心激荡 2024-10-05 17:36:52

您可以使用:

示例:

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

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文