Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to iterate a list like List<Map<String,Object>>

Tags:

java

foreach

I have a method which is returning List<Map<String,Object>>.

How to iterate over a list like List<Map<String,Object>>?

like image 797
jai Avatar asked Apr 26 '11 07:04

jai


People also ask

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.

How do you iterate through a list of objects?

Obtain an iterator to the start of the collection by calling the collection's iterator() method. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true. Within the loop, obtain each element by calling next().

Can we iterate through list?

You can loop through the list items by using a while loop. Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by referring to their indexes. Remember to increase the index by 1 after each iteration.


2 Answers

It sounds like you're looking for something like this:

List<Map<String, Object>> list; // this is what you have already

for (Map<String, Object> map : list) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
    }
}
like image 109
WhiteFang34 Avatar answered Oct 20 '22 20:10

WhiteFang34


List<Map<String, Object>> list = getMyMap();    
for (Map<String, Object> map : list) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " - " + entry.getValue());
    }
}
  1. Loop through list of maps
  2. Handle map entries
like image 30
dertkw Avatar answered Oct 20 '22 21:10

dertkw