Home > Net >  How to flatten nested array of JsonLists
How to flatten nested array of JsonLists

Time:08-17

[[{header=C, value=dsd}, {header=D, value=test}, {header=E, value=e}, {header=F, value=hhh}, {header=G, value=ghgh}]]

Above is an array of arrays of JsonLists and I need to flatten out the outer array to just be the inner array of JsonLists.

I'd also eventually get only the values out of the JsonList and put those values into it's own separate array :

[dsd, test, e, hhh, ghgh]

CodePudding user response:

It's basically similar to flattening an ArrayList of ArrayList.

final List<Map<String, Object>> result = jsonList.stream()
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

and for the values

final List<Object> valueList = result.stream()
                .map(i -> i.get("value"))
                .collect(Collectors.toList());
  •  Tags:  
  • java
  • Related