How get value from LinkedHashMap based on index not on key?

JavaAndroidLinkedhashmap

Java Problem Overview


I have

LinkedHashMap<String, List<String>> hMap;

I want to get List<String> by position not on key.

I don't want to use iterate.

Is there any other way to get Value based on index ?

Java Solutions


Solution 1 - Java

You can't get the value of the Map based on index, Maps just don't work that way. A workaround would be to create a new list from your values and get the value based on index.

LinkedHashMap<String, List<String>> hMap;
List<List<String>> l = new ArrayList<List<String>>(hMap.values());
l.get(0);

Solution 2 - Java

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

Solution 3 - Java

you may want to consider either using another class to store your data, or write an extension to the linkedHashMap. something like

//this is pseudo code
public class IndexedLinkedHashMap<K,V> extends LinkedHashMap{

HashMap<int,K> index;
int curr = 0;

    @Override
    public void add(K key,V val){
        super.add(key,val);
        index.add(curr++, key);
    }

    public V getindexed(int i){
        return super.get(index.get(i));
    }

}

Solution 4 - Java

As Kevin Bowersox stated, it's as simple as

List<String> result = (List<String>) hMap.values().toArray()[position];

But it should be noted that this will still iterate by using .toArray(). It's a simple statement and I'm not sure if there is one with better performance, but be aware that complexity is not log(n) (like indexed access in case of B*), but just n. Since LinkedHashMap is based on LinkedList, there is no way to randomly access elements, only in sequential order.

The cast to List is an unavoidable evil, since .toArray() follows the archaic concept of returning Object instead of a generic data type.

While this might not be the main concept of a map, LinkedHashMap isn't just a map. it extends HashMap, and as an extending class, it's perfectly fine to bring additional methods that support the idiosyncracies of that class.

Solution 5 - Java

There is no direct DS in the standard Java Collections API to provide a indexed map. However, the following should let you achieve the result:

// An ordered map
Map<K, V> map = new LinkedHashMap<K, V>();
// To create indexed list, copy the references into an ArrayList (backed by an array)
List<Entry<K, V>> indexedList = new ArrayList<Map.Entry<K, V>>(map.entrySet());
// Get the i'th term
<Map.Entry<K,V>> entry = indexedList.get(index);
K key = entry.getKey();
V value = entry.getValue();

You might still want to retain the concerns of data persistence in the map separate from the retrieval.

Update: Or use LinkedMap from Apache Commons.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMACView Question on Stackoverflow
Solution 1 - JavaPermGenErrorView Answer on Stackoverflow
Solution 2 - JavaKevin BowersoxView Answer on Stackoverflow
Solution 3 - JavaschippiView Answer on Stackoverflow
Solution 4 - JavamakromView Answer on Stackoverflow
Solution 5 - JavaNeelView Answer on Stackoverflow