java list<map<string, object>> - Joining a List<String> inside a map
1
Answers
Simply use String.join
, no need to create the nested stream:
Map<String, String> result = map.entrySet()
.stream()
.collect(toMap(
e -> e.getKey(),
e -> String.join("|", e.getValue())));
example iterate over
I'm trying to convert a Map<String, List<String>>
to a Map<String, String>
, where the value for each key is the joint string built by joining all the values in the List
in the previous map, e.g.:
A -> ["foo", "bar", "baz"]
B -> ["one", "two", "three"]
should be converted to
A -> "foo|bar|baz"
B -> "one|two|three"
What's the idiomatic way to do this using the Java 8 Streams API?
27 votes
java