Have a LinkedHashMap<Float, Integer>
. The keys of this HashMap are points in time in seconds (like 0.031f
or 1.4021f
). There can be hundreds of thousands of entries.
Given a float
value, say 0.6102f
, I need to find the HashMap key that is closest to it (like 0.6002f
maybe, assuming that's in the keyset).
A trivial answer would be to check key by key of the HashMap. After all, the entries happen to be correctly sorted. But I guess that's an O(n)
operation which may not be a great idea given that I have to perform this search several times per second.
Is there an efficient way to find the "closest float key" of a HashMap given a float?