Use Java8 to merge List > into a Map?

  java, question

Examples are as follows:

Map<String,Object> h1 = new HashMap<>();
 h1.put("12","fdsa");
 h1.put("123","fdsa");
 h1.put("124","fdsa");
 h1.put("125","fdsa");
 
 Map<String,Object> h2 = new HashMap<>();
 h2.put("h12","fdsa");
 h2.put("h123","fdsa");
 h2.put("h124","fdsa");
 h2.put("h125","fdsa");
 
 Map<String,Object> h3 = new HashMap<>();
 h3.put("h312","fdsa");
 h3.put("h3123","fdsa");
 h3.put("h3124","fdsa");
 h3.put("h3125","fdsa");
 
 List<Map<String,Object>> lists = new ArrayList<>();
 lists.add(h1);
 lists.add(h2);
 lists.add(h3);

Want to use java 8 to merge the maps in lists into a new map:

Map<String,Object> haNew = new HashMap<>(); //contains h1,h2,h3

Please tell me how to realize it, thank you.

I implemented one myself in the following way:

private Map<String,Object> megerListMap(List<Map<String,Object>> listsMap){
 Map<String,Object> map = new HashMap<>();
 listsMap.forEach(x->{
 x.forEach((y,z)->{
 map.put(y, z);
 });
 });
 return map;
 bracket

But I want a simpler way.

References
https://stackoverflow.com/que …

Map<String, Object> merged = lists.stream()
 .map(Map::entrySet)
 .flatMap(Set::stream)
 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));