I have a map
like
key= ["a1", "a2", "a3"]
value = [["a1.value1", "a1.value2"],["a2.value1", "a2.value2"]]
the resulting Map should be like
key = ["a1", "a2", "a3"]
value = ["a1.value1, a1.value2", "a2.value1, a2.value2"]
How can we use Collectors.joining
as an intermediate step ?
How can we use Collectors.joining as an intermediate step ?
You mean, in the collecting phase...
Yes, you can:
Map<String, String> result =
source.entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
e -> e.getValue().stream().collect(joining(", "))));
but , better to use String.join
:
Map<String, String> result =
source.entrySet()
.stream()
.collect(toMap(Map.Entry::getKey, e -> String.join(", ", e.getValue())));
or none stream variant:
Map<String, String> resultSet = new HashMap<>();
source.forEach((k, v) -> resultSet.put(k, String.join(",", v)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.