开发者

how to transform Map<String,String> to List<String> using google collections

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 want to do using google collections:

public static List<String> addLstOfSetEnvVariables(Map<String, String> env)
{
    ArrayList<String> result = Lists.newArrayList();
    for (Entry<String, String> entry : env.entrySet())
    {
        result.add(entry.getKey() + " " + entry.getValue());
    }
    return result;
}


Here you go:

private static final Joiner JOINER = Joiner.on(' ');
public List<String> mapToList(final Map<String, String> input){
    return Lists.newArrayList(
        Iterables.transform(
            input.entrySet(), new Function<Map.Entry<String, String>, String>(){
                @Override
                public String apply(final Map.Entry<String, String> input){
                    return JOINER.join(input.getKey(), input.getValue());
                }
            }));
}

Update: optimized code. Using a Joiner constant should be much faster than String.concat()


These days, I would of course do this with Java 8 streams. No external lib needed.

public List<String> mapToList(final Map<String, String> input) {
    return input.entrySet()
                .stream()
                .map(e -> new StringBuilder(
                             e.getKey().length()
                                     + e.getValue().length()
                                     + 1
                     ).append(e.getKey())
                      .append(' ')
                      .append(e.getValue())
                      .toString()
                )
                .collect(Collectors.toList());
}


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<String> mapToList(Map<String, String> env) {
    List<String> result = new ArrayList<String>();
    for (Entry<String, String> 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.


Here's a functional approach using Java 8 streams:

List<String> 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<String> kv = new ArrayList<>();
map.forEach((k, v) -> kv.add(k + " " + v));


More Current solution

public static void main(final String[] args)
{
    final Map<String, String> m = ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3");
    final Collection<String> c = Maps.transformEntries(m, new Maps.EntryTransformer<String, String, String>()
    {
        @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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜