I am looking for a Map implementation that returns the value associated with the requested key, or if not present returns the closest value, higher or lower as requested (along with the actual key, perhaps as a Map.Entry).
For example if the Map contained the following String key/value pairs:
alpha:AYE, beta:BEE, charlie:CEE, delta:DEE
and you ask for “Next higher” for “canada” you would get back charlie:CEE
Of course if you ask for the Next higher or Next lower for “charlie” you would get back charlie:CEE
It should use a comparator, so that if it contains Number keys 1, 2, 3 and I request Next higher for 1.4, it would return the 2 key.
Use a NavigableMap:
http://docs.oracle.com/javase/6/docs/api/java/util/NavigableMap.html
In particular, use floorEntry or ceilingEntry or a combination.
TreeMap is an instance of NavigableMap, so you can use that:
http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html