how to transform Map to List using google collections

后端 未结 4 2070
抹茶落季
抹茶落季 2021-02-14 02:01

I have a map with strings, I want to transform it to a list of strings with \" \" as a key value separator. Is it possible using google collections?

Code example that I

4条回答
  •  逝去的感伤
    2021-02-14 02:25

    Here's a functional approach using Java 8 streams:

    List kv = map.entrySet().stream()
        .map(e -> e.getKey() + " " + e.getValue()) //or String.format if you prefer
        .collect(Collectors.toList());
    

    If you're not wedded to the functional style, here's a more concise variant of the obvious for loop:

    List kv = new ArrayList<>();
    map.forEach((k, v) -> kv.add(k + " " + v));
    

提交回复
热议问题