Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value stored in ArrayList<HashMap<key,value>>?

I have ArrayList>. In another activity I want to access all values stored in ArrayList>.

I have tried following code:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

for(Hashmap<String, String> map: mylist) {
    for(Entry<String, String> mapEntry: map) {
        String key = mapEntry.getKey();
        String value = mapEntry.getValue();
    }
}

but it shows an error at for(Entry<String, String> mapEntry: map) that it only interate over Array.

like image 555
Roshni Kyada Avatar asked Jun 29 '12 09:06

Roshni Kyada


People also ask

How do you get a value from an ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.

How do you retrieve all key-value pairs present in a HashMap?

To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.

Can the value of a HashMap be an ArrayList?

A HashMap contains key-value pairs, there are three ways to convert a HashMap to an ArrayList: Converting the HashMap keys into an ArrayList. Converting the HashMap values into an ArrayList. Converting the HashMap key-value pairs into an ArrayList.


1 Answers

Your code has bit different for this line,

for(Entry<String, String> mapEntry: map.entrySet())

Try this and let me know what happen,

for (HashMap<String, String> map : mylist)
     for (Entry<String, String> mapEntry : map.entrySet())
        {
        String key = mapEntry.getKey();
        String value = mapEntry.getValue();
        }
like image 172
user370305 Avatar answered Nov 14 '22 16:11

user370305