java - Float as key in map -


given collection of {price, quantity} pair, need aggregate quantities @ same price point. easiest way achieve it?

a dirty-cut, implement solution using hashmap keys being price , value being aggregated quantity. but, in knowledge, float isn't safe key hashmap. solution error-prone.

what recommended alternative solve problem?

you use bigdecimal key. hashcode safe.

you'd have initialize values have same scale, instance:

bigdecimal key = new bigdecimal(double.tostring(price)).setscale(2); 

Comments

Popular posts from this blog

symfony - InvalidConfigurationException in SecurityExtension.php line 429: No authentication listener registered for firewall "secured_area" -

angular ui router - 10 digest iterations reached in angularjs when using $state.go -

javascript - Handling constructor related functions while testing with mocha and sinon -