循环遍历hashMap的键

时间:2014-01-31 17:39:23

标签: java hashmap

我有一个叫做AfricanPeople的HashMap

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

关键是年龄,值是人物对象。

我想循环遍历哈希映射键,让所有年龄介于30到45岁之间的人。

这可能吗?

1 个答案:

答案 0 :(得分:3)

可以,使用

for (Integer key : map.keySet())

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

相关问题