简体   繁体   中英

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. ie < ( , { = : . + @ 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM