Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get value from LinkedHashMap based on index not on key? [duplicate]

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 ?

like image 962
MAC Avatar asked Nov 27 '12 10:11

MAC


People also ask

How do I get the index value in LinkedHashMap?

Method 1(Using keys array): You can convert all the keys of LinkedHashMap to a set using Keyset method and then convert the set to an array by using toArray method now using array index access the key and get the value from LinkedHashMap.

Can we access HashMap using index?

Arrays store items in an ordered collection and are accessed using an index number (which is an integer). HashMap stores items as key/value pairs. Values can be accessed by indexes, known as keys, of a user-defined type.

How can I tell if a LinkedHashMap contains a key?

LinkedHashMap. containsKey() method is used to check whether a particular key is being mapped into the LinkedHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.


1 Answers

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); 
like image 111
PermGenError Avatar answered Sep 20 '22 06:09

PermGenError