如何迭代 的映射?
我有一个 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,但我有点不确定如何获取密钥
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
entrySet() 怎么样
What about entrySet()
您可以使用:
示例:
You can use:
Example: