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

18,617

Solution 1

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());

Solution 2

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

Share:
18,617
Piyush
Author by

Piyush

i like android

Updated on June 07, 2022

Comments

  • Piyush
    Piyush almost 2 years

    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.

  • Ben van Gompel
    Ben van Gompel over 12 years
    For the 2nd iteration I personally prefer to iterate of the map entries, instead of the keys. for (Entry<String, String> entry : map.entrySet()). That way you already both the key and the value, you don't need another map lookup.
  • dacwe
    dacwe over 12 years
    @BenvanGompel: Good point, depends on your use case, but updated! :)