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
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());
}
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));
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.
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);
}
[k1 v1, k2 v2, k3 v3]