Skip to content Skip to sidebar Skip to footer

Best Java Collection To Store Key, Value Pairs And Access Its Objects By Index Or By Key Name

I want to store a key,values pair that allows me to get the values by its position (ordering must persist) or by its key name. I was thinking to use HashMap but i do not want to it

Solution 1:

You can use LinkedHashMap. It maintains the inserted order. You cannot get by index as they all implement the Map interface. You can do this way-

public List<String> getByIndex(final LinkedHashMap<String, List<String>> myMap, final int index){
   return (List<String>)myMap.values().toArray()[index];
}

Post a Comment for "Best Java Collection To Store Key, Value Pairs And Access Its Objects By Index Or By Key Name"