Iterating nested Structure of Maps and Lists

sass nested lists
sass nested maps
iterate map in java 8 using stream
sass maps
sass loop through map
sass list
sass color map
sass each

I have the following HashMap map, containing the ArrayList content, containing several HashMaps:

{
   key1=val1,
   key2=val2,
   // ...
   content=   [
      {
         keyAbc=val10,
         keyDef=val11,
         KeyGhi=val12,
      },
      {
         keyAbc=val13,
         keyDef=val14,
         KeyGhi=val15
      },
      {
         keyAbc=val16,
         keyDef=val17,
         KeyGhi=val18
      }
   ],
   key20=val20,
   // ...
}

Now I need to get all keys and values content.


  1. Getting into the ArrayList

When I try

for(int i=0; i<map.get("content").size(); i++){}

it says cannot find symbol, as if it doesn't recognize the ArrayList.

However, casting it as an ArrayList works:

for(int i=0; i<((ArrayList)map.get("content")).size(); i++){}

  1. Getting into the HashMap

So within that loop I try:

// for(...){
   for(Map.Entry<Object, Object> obj : (((ArrayList)map.get("content")).get(i)){}
}

The following error appears:

for-each not applicable to expression type required: array or java.lang.Iterable found: Object

Casting as a HashMap:

// for(...){
   for(Map.Entry<Object, Object> obj : ((HashMap)(ArrayList)map.get("content")).get(i)){}
}

Produces that error:

incompatible types: ArrayList cannot be converted to HashMap


So how can I access the contents of content?

You want to iterate over the entries of the Map, so you should call entrySet:

for (Map.Entry<Object, Object> obj : ((HashMap)((ArrayList)map.get("content")).get(i)).entrySet() {}

To make it more readable, I'd break it into multiple statements. I'd also use the interface types instead of ArrayList and HashMap.

List list = (List) map.get("content");
for (int i = 0; i < list.size(); i++) {
    Map map = (Map) list.get(i);
    for (Map.Entry<Object,Object> entry : map.entrySet()) {

    }
}

Elixir Iteraptor :: Iterating Nested Terms Like I'm Five, The tiny elixir library to iterate/map/filter deeply nested structures in Elixir. Iterating both maps and lists in Elixir is charming. One might chain  I have a nested map of unknown structure, the goal is to iterate through every value in the map, check the values of a certain condition (e.g. null) and replace those values with something else. I can see how to do it if the map structure is known, but the issue here is that it is unknown.

I will use more code to have a more human readable version of it and I replace concrete classes with interfaces (you can't be sure that the deserializer will create an ArrayList instead of another kind of List, same for Map) having the following code:

List content = (List) map.get("content");
for (Object item : content) {
  Map itemMap = (Map) item;
  for (Map.Entry entry : itemMap.entrySet()) {
     // Doing what you like with entry  
  }  
}

Sass Maps vs. Nested Lists, Let's compare a map and a nested list to see how they compare in their own For our example, let's create a data structure that controls responsive typography. That loop will iterate four times, once for each nested map. So, welcome the library that makes the iteration of any nested map/keyword/list combination almost as easy as the natural Elixir map and each. • Iteraptor. Features. Iteraptor.each/3 to iterate a deeply nested map/list/keyword; Iteraptor.map/3 to map a deeply nested map/list/keyword; Iteraptor.reduce/4 to reduce a deeply nested map/list/keyword;

You have to iterate through list which is value content key. I suppose below code is what you are looking for.

         for (Map<Object, Object> listEntry : (List<Map<Object, Object>>) map.get("content")) {
        for (Map.Entry innerMapEtry : listEntry.entrySet()) {
            final Object key = innerMapEtry.getKey();
            final Object value = innerMapEtry.getValue();
        }
    }

6 ways to iterate or loop a Map in Java, Using foreach in Java 8. If you using Java 8 this is the easiest way to loop the Map. 1. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 27. // {. Map<Integer  List Comprehensions are one of the most amazing features of Python. It is a smart and concise way of creating lists by iterating over an iterable object. Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops.

Replace Nested Loops with flatMap, Nested For-Loops or invocations of forEach commonly used to iterate over all elements of a collection of which are used to iterate over such data structures and replaces them with single invocations of Stream::flatMap . List<List<User>​> groups = findGroups(); groups. orders.stream() .map(Order::getOrderItems) . A friend of mine has been doing a lot of work analyzing hydrologic time series from a large number of stream gauges in California. She’s been doing all kinds of statistical analyses with various transformations and combinations of the dataset, which has inevitably led to working with nested lists of horrifying proportions.

The Map Interface (The Java™ Tutorials > Collections > Interfaces), Classify Person objects by city Map<String, List<Person>> peopleByCity The Map interface provides a small nested interface called Map.Entry , the type of the Filter a map based on some // property of its keys. for (Iterator<Type> it = m. JSON structure #4 : Nested structures with Lists. Now we iterate over the list and map each object in list to Image by calling Image.fromJson and then we put each map object into a new list

produce map/object from nested for loop in terraform >0.12 · Issue , produce map/object from nested for loop in terraform >0.12 #22263 While I found some examples on how to produce a list of maps, I am a clever way to generate the structure you need in the community forum where there  To: 'Struts Users Mailing List' Subject: Iterating over a tree structure I have been racking my brains out today on this one. What is the best way to perform a depth first traversal of a tree data structure (in the view). Ex: maps of maps of OR lists of lists of lists of

Comments
  • You have a problem with your declared types. These type casts are going to be necessary unless you use a proper class for your object. Is this deserialized JSON?
  • @ernest_k: Yes, it is.
  • @user1170330 can you post the json instead of the jvm inspect of the json?