Is there a elegant way of obtaining only one Entry<K,V> from HashMap, without iterating, if key is not known.
As order of entry of entry is not important, can we say something like
hashMapObject.get(zeroth_index);
Although I am aware that there exist no such get by index method.
If I tried approach mentioned below, it would still have to get all the entry set of the hashmap.
for(Map.Entry<String, String> entry : MapObj.entrySet()) {
return entry;
}
Suggestions are welcome.
EDIT: Please suggest any other Data Structure to suffice requirement.
The answer by Jesper is good. An other solution is to use TreeMap (you asked for other data structures).
TreeMap has an overhead so HashMap is faster, but just as an example of an alternative solution.