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

javascript - Can Piwik report referrer in real time? -

asp.net - Google Drive Access: At least one client secrets (Installed or Web) should be set -

excel vba add hyperlink to shape in group -