Combining String values ​​of all Maps in a list

I am trying to adapt the functions of Lambda, however there is a bit of a struggle here and there.

List<Map<String, String>> list = new LinkedList<>();
Map<String, String> map = new HashMap<>();
map.put("data1", "12345");
map.put("data2", "45678");
list.add(map);

I just want to print the values ​​in comma separated format, like 12345,45678

So here is my test

list.stream().map(Map::values).collect(Collectors.toList()) //Collectors.joining(",")

and the exit is [[12345,45678]]. This means that the list and internal list create a comma separated value in the index 0. I understand why he does it.

But I did not understand how to extract the desired result if I did not name .get(0)at the end of this expression.

Any help / more details on how to use lambdas better would be helpful

+4
source share
1 answer

Try the following:

list.stream().map(Map::values).flatMap(Collection::stream).collect(Collectors.joining(","))

The method flatMapaligns a Collection<Stream<String>>to one Stream<String>.

, @Holger .

, , Map, , . 45678,12345 . , LinkedHashMap HashMap.

+7

All Articles