You can not. From the HashMap JavaDocs :
Implementation of a map interface based on a hash table. This implementation provides all optional card operations and allows null values ββand a null key. (The HashMap class is roughly equivalent to the Hashtable, except that it is unsynchronized and resolves to null.) This class gives no guarantees regarding the order of the map; in particular, it does not guarantee that order will remain constant over time.
Thus, the order can vary between iterations. If you need to save an order, you can take a look at LinkedHashMap
From LinkedHashMap JavaDocs :
A hash table and a linked list of the map interface with a predictable iteration order. This implementation differs from HashMap in that it supports a doubly linked list passing through all its entries. This linked list defines the iteration order, which is usually the order in which keys were inserted into the map (insert order).
So, to find the key position, you basically need to iterate over the keys and count the position of the key you are looking for.
On the side of the IMO note, this may not be the best way to use the Map data type. I believe that if you really need a position, you should use some type of List (for example, ArrayList ) that actually keeps order, and you can use the get method to retrieve the elements for a specific index.
source share