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.
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.
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.
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.
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());
for(HashMap<String, String> map : data){
... deal with map...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With