如何使用Eclipse集合将MutableMap<String, Double>转换为ObjectDoubleMap<String>

在我的用例中,我有一个可变的映射,它是汇总结果。例如;

MutableMap<String, Double> map = list.aggregateBy(func, factory, aggregator);


我必须调用另一个仅接受ObjectDoubleMap<String>的方法,如何将map转换为ObjectDoubleMap<String>类型?我别无选择,只能使用Eclipse Collections框架。

最佳答案

首先,感谢您使用Eclipse Collections!当前,将MutableMap转换为ObjectDoubleMap的最佳方法是使用forEachKeyValue()进行迭代,并将键值放入空的MutableObjectDoubleMap

MutableMap<String, Double> stringDoubleMutableMap = Maps.mutable.with("1", 1.0, "2", 2.0);
MutableObjectDoubleMap<String> targetMap = ObjectDoubleMaps.mutable.empty();
stringDoubleMutableMap.forEachKeyValue((key, value) -> targetMap.put(key, value));


将来我们可以考虑添加一个API,它将使从RichIterable<BoxedPrimitive>PrimitiveIterable的转换更加容易。

随时使用您想要的API在Eclipse Collections GitHub Repo上打开问题。这有助于我们跟踪用户的功能请求。 Eclipse Collections也开放供您贡献,请随时贡献您想要的API:Contribution Guide

更新了注释的答案,lambda可以简化为方法参考:

stringDoubleMutableMap.forEachKeyValue(targetMap::put);


注意:我是Eclipse Collections的提交者。

关于java - 如何使用Eclipse集合将MutableMap转换为ObjectDoubleMap?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49593558/

10-14 12:04
查看更多