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
Post a Comment