How to convert Map<String,String> to "key1:value1,key2:value2,.." using lambda
dictionary, java, java-8, lambda
Solution
You can use a map and a joining collector
Map<String,String> map = new LinkedHashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
String text = map.entrySet().stream()
.map(e -> e.getKey() + ":" + e.getValue())
.collect(Collectors.joining(","));
System.out.println(text);
prints
key1:value1,key2:value2,key3:value3
BTW I wouldn't use String.format() if you can avoid it as it can be much slower than String concatenation.
Problem
I have a `Map<String,String>` and I need to convert it to a single `String`: `"key1:value1,key2:value2,..."`. I can do this using iteration. But can I do this using new lambda and stream functionality of java 8? Thanks!