Looping through the keys of a hashMap
I have a HashMap called AfricanPeople
private HashMap<Integer, Object> AfricanPeople = new HashMap<Integer, Object>();
the key is the age and the value is a person object.
I want to loop through the hashmaps keys and get all people that are between the age of 30 and 45.
Is this possible?
It is possible, using
for (Integer key : map.keySet())
But a HashMap is not the appropriate structure for this. You should use a TreeMap instead, which can directly return a submap containing the keys between 30 and 45: