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

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -