Java Question

Get key from HashMap in Android by position or index

I have:

public static HashMap<String, String> CHILD_NAME_DOB = new HashMap<>();

Suppose the values in


I am trying to fetch the last key element from
. That is, I want to fetch key
from the example above to temporary
String name

Also I want to know on how to fetch data by index.

Eg.: if
int index = 2
, I want key
String name


Edit: DateOfBirth value (value data in
) is dynamic and is unknown. So THIS LINK is not what I want.

Answer Source

Thanks to @Pentium10 for this answer. And I little modified it according to my need.

String key="default";
Iterator myVeryOwnIterator = CHILD_NAME_DOB.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
     //String value=(String)meMap.get(key);
Toast.makeText(viewEnterChildExp.getContext(), "Key: "+key , Toast.LENGTH_LONG).show();

I'm getting the last key element by this.

I'll update as soon I also get to find an easy way to key by index.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download