generating map from list of objects having a map using java lambda8

  • A+
Category:Languages

I've an object,

class Object2{      String name;      String id;      Map<String, String> customData; }  class Object1{      List<Object2> obj1List; } 

I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.

 


Here's a way with lambdas and Java 8:

Map<String, String> map = new LinkedHashMap<>(); object1List.forEach(o1 ->          o1.getObject1List().forEach(o2 -> map.putAll(o2.getCustomData()))); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: