Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate Arraylist<HashMap<String,String>>?

I have an ArrayList object like this:

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

How to iterate through the list? I want to display the value in a TextView which comes from the data of ArrayList object.

like image 276
Piyush Avatar asked Oct 07 '11 06:10

Piyush


People also ask

How do I iterate over a map string ArrayList object >>?

Different ways to iterate through Map : Using keySet(); method and Iterator interface. Using entrySet(); method and for-each loop. Using entrySet(); method and Iterator interface. Using forEach(); in Java 1.8 version.

How do I iterate a String object on a map?

Iterating over Map. Entry<K, V>>) of the mappings contained in this map. So we can iterate over key-value pair using getKey() and getValue() methods of Map. Entry<K, V>. This method is most common and should be used if you need both map keys and values in the loop.

Do you know HashMap How do you iterate keys and values in HashMap?

Example 2: Iterate through HashMap using iterator() In the above example, we are iterating through keys, values, and key/value mappings of the hash map. We have used the iterator() method to iterate over the hashmap. Here, hasNext() - returns true if there is next element in the hashmap.


2 Answers

Simplest is to iterate over all the HashMaps in the ArrayList and then iterate over all the keys in the Map:

TextView view = (TextView) view.findViewById(R.id.view);

for (HashMap<String, String> map : data)
     for (Entry<String, String> entry : map.entrySet())
         view.append(entry.getKey() + " => " + entry.getValue());
like image 109
dacwe Avatar answered Sep 21 '22 06:09

dacwe


for(HashMap<String, String> map : data){ ... deal with map... }

like image 30
DallinDyer Avatar answered Sep 21 '22 06:09

DallinDyer