Can you please help me to select the score value which must be rounded down to the nearest score if its not found.
I need to write a method taking score as input and return its value, if the value is not found need to return the nearest value
i.e suppose if the input value is 0.49 then its needs to return 80.
suppose if the input value is 0.94534 then we dont have the value in the map. So then since the input is <1 but >0.72 so it should consider 0.72 as input and return 90.
That thread is somthing different. Its just acting like a counter.
My problem is:
Suppose if I pass 0.49 to method displayRating() then its should display 80
suppose if I pass 0.94534 to method displayRating() then its should display 90, because we dont have the value in the map for 0.94534. So it should consider a value 0.94534<1 but 0.94534>0.72
I'd suggest you convert the keySet of your map to a TreeSet and then apply floor() or lower() [Note : both are different] on it and narrow down on a particular key.
There you go - fetch the corresponding value from the Map.
Somewhere, in code that you didn't show us, you are casting the keySet of a hash map to a TreeSet. That is not going to work. I guess you did that because of Vinoth's suggestion. But converting the keySet to a TreeSet is not the same as casting it. In fact, casting something doesn't do any kind of conversion at all. It just tells the compiler "I have this object here, you should treat it as if it is a TreeSet". If then, at runtime, the object turns out not to be a TreeSet, you'll get a ClassCastException.
What you could do is use a TreeMap instead of a HashMap. Class TreeMap contains the lowerKey method that you're looking for.
By the way, using floating-point types such as Double as the keys in a map is not a good idea. Floating-point types are inherently imprecise, and maps are really made to lookup things with identifiable, exact keys.
Actually, what I meant by converting the keySet into a TreeSet is that, use the constructor of TreeSet that takes in a collection & pass the keySet to it.
Ahh..yes...I forgot TreeMap has such methods too...as it implements NavigableMap.
Rauf, as Jesper says, you can go directly for a TreeMap, rather than taking the keySet and converting it to a TreeSet..blah blah..