how to transform Map to List using google collections

后端 未结 4 2062
抹茶落季
抹茶落季 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:35

    More Current solution

    public static void main(final String[] args)
    {
        final Map m = ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3");
        final Collection c = Maps.transformEntries(m, new Maps.EntryTransformer()
        {
            @Override public String transformEntry(@Nullable final String key, @Nullable final String value)
            {
                return Joiner.on(' ').join(key, value);
            }
        }).values();
        System.out.println(c);
    }
    

    Outputs

    [k1 v1, k2 v2, k3 v3]
    

提交回复
热议问题