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?

Answers


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:


Need Your Help

how to answer HLD and LLD

oop amazon

I have recently attended Amazon Interview and got rejected after design round, its happened three times with Amazon in 2 year, i always getting rejected because of design round.

How can I show a message after uninstalling a WiX Burn bundle?

wix burn

How can I add a message to the successfully uninstalled page of a Burn bundle?