Reputation: 2228
How to convert MutableMap<String, Double>
to ObjectDoubleMap<String>
using Eclipse Collections?
In my use case, I have a mutable map which is aggregated result. For example;
MutableMap<String, Double> map = list.aggregateBy(func, factory, aggregator);
I have to call another method which only accepts ObjectDoubleMap<String>
, how can I convert map
to a type of ObjectDoubleMap<String>
? I have no choice but to use Eclipse Collections framework.
Upvotes: 0
Views: 264
Reputation: 1152
Firstly, thanks for using Eclipse Collections! Currently, the best way to convert a MutableMap
to ObjectDoubleMap
is by iterating using forEachKeyValue()
and putting the key, value to an empty 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));
In the future we can look at adding an API which will make the conversion from RichIterable<BoxedPrimitive>
to PrimitiveIterable
easier.
Feel free to open issues on Eclipse Collections GitHub Repo with the API which you would like to have. This helps us track feature requests by our users. Eclipse Collections is open for contributions as well, feel free to contribute the API which you would like: Contribution Guide.
Updated answer from comments, the lambda can be simplified to a method reference:
stringDoubleMutableMap.forEachKeyValue(targetMap::put);
Note: I am a committer on Eclipse Collections.
Upvotes: 1