0

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.

fabfas
  • 2,200
  • 1
  • 21
  • 21

1 Answers1

1

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.

Nikhil Nanivadekar
  • 1,152
  • 11
  • 10
  • Thanks, it is long-winded way of adapting the interface for primitives, but it works nonetheless. – fabfas Apr 01 '18 at 17:21
  • Like I mentioned in the answer, if you feel that there is a conversion API we should have feel free to propose on our GitHub Repo. Also, don't forget to accept the answer. – Nikhil Nanivadekar Apr 02 '18 at 05:51
  • Accepted! It would be nice to have a simpler API – fabfas Apr 02 '18 at 11:40
  • 1
    This can be simplified slightly using a method reference instead of a lambda - `stringDoubleMutableMap.forEachKeyValue(targetMap::put);` – Donald Raab Sep 26 '18 at 02:40