after I create a new object : Map<Float,Integer> m = new HashMap()<Float,Integer>;
I have an array with "float"
numbers which are unique . I want to add these float numbers as the m's key
! can I do this like : m.put(new Float(a[i]),"cat");
?
thanks
I would recommend against using floating point numbers as hash map keys if possible. Your keys must match exactly when you look up values, and it's easy to get floating point numbers that aren't exactly right. That's why in these situations precision is being used i.e.
algorithm: Add first 5 values to an array, sort the array, iterate through the map until you find a value higher than the first value in the array (the lowest), replace the lowest, resort the array (in case the new value is higher than the others), continue.
Using doubles as keys is not useful. As soon as you make any arithmetic on the keys you are not sure what exact values they have and hence cannot use them for indexing the map. The only sensible usage would be that the keys are constant.
I would recommend against using floating point numbers as hash map keys if possible. Your keys must match exactly when you look up values, and it's easy to get floating point numbers that aren't exactly right.
Just to take all answers together.
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