how to transform Map to List using google collections

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

    Functional programming is cool, but in Java often adds complexity you really shouldn't be adding (as Java doesn't support it very well) I would suggest you use a simple loop which is much shorter, more efficient and eaiser to maintain and doesn't require an additional library to learn.

    public static List mapToList(Map env) {
        List result = new ArrayList();
        for (Entry entry : env.entrySet())
            result.add(entry.getKey() + " " + entry.getValue());
        return result;
    }
    

    A simple test of code complexity it to count the number of symbols used. i.e. < ( , { = : . + @ Not counting close brackets.

    plain loop 22 symbols.
    functional approach 30 symbols.
    

提交回复
热议问题