As you want to get a List, not a map, your last function call cannot be Collectors.toMap
, needs to be Collectors.toList
. Now, each invocation to the map
method should generate a new Map, so something like this would do:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
Map<String, String> map = new HashMap<>();
map.put("name", parts[0]);
map.put("display", parts[1]);
return map;
)
.collect(Collectors.toList());
Some people would prefer:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
return new HashMap<>() {{
put("name", parts[0]);
put("display", parts[1]);
}};
)
.collect(Collectors.toList());
which creates an extra helper class. Or if you can use Guava:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
return ImmutableMap.of("name", parts[0], "display", parts[1]);
)
.collect(Collectors.toList());
BTW: In the examples I used List
but the complete type of what you describe would be List<Map<String, String>>
.