我有一个Map<String,String>
,我需要将它转换为一个String
:"key1:value1,key2:value2,..."
。
我可以使用迭代来做到这一点。但是我可以使用Java 8的新lambda和流功能来做到这一点吗?谢谢!
您可以使用地图和加入收集器
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);
版画
key1:value1,key2:value2,key3:value3
顺便说一句,如果可以避免的话,我不会使用String.format(),因为它比String串联要慢得多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句