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())));
                        Use flatMap and toMap as follows:
List<Object1> source = ...
Map<String, String> result = 
     source.stream()
           .flatMap(e -> e.getObj1List().stream()
                               .flatMap(a -> a.getCustomData().entrySet().stream()))
           .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
or if you're dealing with a single Object1 object:
Object1 myObj = ...
Map<String, String> result = 
      myObj.getObj1List()
           .stream()
           .flatMap(a -> a.getCustomData().entrySet().stream())
           .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
                        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