Is it possible to get element from HashMap by its position?

后端 未结 14 1324
时光说笑
时光说笑 2020-12-12 18:31

How to retrieve an element from HashMap by its position, is it possible at all?

14条回答
  •  时光说笑
    2020-12-12 19:26

    If you, for some reason, have to stick with the hashMap, you can convert the keySet to an array and index the keys in the array to get the values in the map like so:

    Object[] keys = map.keySet().toArray();
    

    You can then access the map like:

    map.get(keys[i]);
    

提交回复
热议问题