【问题标题】:Looping through the keys of a hashMap [closed]遍历hashMap的键[关闭]
【发布时间】:2014-02-24 12:43:12
【问题描述】:

我有一个名为 AfricaPeople 的 HashMap

private HashMap<Integer, Object> AfricanPeople = new HashMap<Integer, Object>();

键是年龄,值是人对象。

我想遍历 hashmaps 键并获取所有年龄在 30 到 45 岁之间的人。

这可能吗?

【问题讨论】:

  • 请阅读 javadoc。在谷歌中输入HashMap...
  • 或者,更好的是,将the documentation 加入书签,以便下次找到。
  • 请始终包含您迄今为止尝试过的信息...

标签: java hashmap


【解决方案1】:

有可能,使用

for (Integer key : map.keySet())

但是 HashMap 不是合适的结构。您应该改用 TreeMap,它可以 directly return a submap 包含 30 到 45 之间的键:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2011-10-23
    • 2020-03-21
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多