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's a functional approach using Java 8 streams:
If you're not wedded to the functional style, here's a more concise variant of the obvious for loop:
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.
A simple test of code complexity it to count the number of symbols used. i.e. < ( , { = : . + @ Not counting close brackets.
More Current solution
Outputs
Here you go:
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.