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
CHILD_NAME_DOB
are:

<adam,15121990>
<roy,01051995>
<neha,05091992>
<alisha,11051992>


I am trying to fetch the last key element from
CHILD_NAME_DOB
. That is, I want to fetch key
alisha
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
"Neha"
in
String name


TIA.

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

Answer

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()) {
     key=(String)myVeryOwnIterator.next();
     //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.

Comments