Given a collection of {Price, Quantity}
pair, I need to aggregate all quantities at same price point. What is the easiest way to achieve it?
A dirty-cut, would be to implement solution using Hashmap
with keys being price and value being aggregated quantity. But, in my knowledge, Float
isn't a safe key for Hashmap
. So this solution is error-prone.
What is recommended alternative to solve this problem?
put(new Float(a[i]),"cat"); because you value has to be an Integer . To do that you have to declare it as Map<Float,String> m = new HashMap()<Float,String>; .
You must have an std::pair to be used as your key, and that means following what @andre just commented.
These two can be any C++ container class or user-defined class or some other valid class(capable of holding values). So, you can use pair as a key in a map as follows: map<pair<int,string> , long> mp; mp.
When you index a map in Go you get two return values; the second one (which is optional) is a boolean that indicates if the key exists. If the key doesn't exist, the first value will be the default zero value.
You could use BigDecimal as a key. It would be hashcode safe.
You'd have to initialize the values so they have the same scale, for instance:
BigDecimal key = new BigDecimal(Double.toString(price)).setScale(2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With