[英]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: 我想用google集合做的代码示例:
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() 使用Joiner常量应该比String.concat()快得多
These days, I would of course do this with Java 8 streams. 这些天,我当然会用Java 8流做这件事。 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. 函数式编程很酷,但是在Java中经常会增加你真正不应该添加的复杂性(因为Java不能很好地支持它)我建议你使用一个简单的循环,这个循环更短,更高效,更易于维护和操作不需要额外的图书馆来学习。
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. ie <(,{=:。+ @不算括号。
plain loop 22 symbols.
functional approach 30 symbols.
Here's a functional approach using Java 8 streams: 这是使用Java 8流的功能方法:
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: 如果你没有坚持功能风格,这里有一个更明确的for循环的简洁变体:
List<String> kv = new ArrayList<>();
map.forEach((k, v) -> kv.add(k + " " + v));
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.