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

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -