I query the DB for two columns where the first one is the key to the second one. How can I convert the resulting list to a single map? Is it even possible? I have just seen
The problem is you have a list of maps. The code below should work:
Map result = new HashMap<>();
steps.stream().forEach(map -> {
result.putAll(map.entrySet().stream()
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> (String) entry.getValue())));
});
If we try to run this example
Map steps1 = new HashMap<>();
steps1.put("key11", "value11");
steps1.put("key12", "value12");
Map steps2 = new HashMap<>();
steps2.put("key21", "value21");
steps2.put("key22", "value22");
List
It happily gives us the output like that:
{key12=value12, key11=value11, key22=value22, key21=value21}