I am using Java for this program, and I currently have a situation where I want to add key/value pairs to a table with integer keys, like
add (1, "Bobby")
add (6, "Sue")
add (3, "Mary")
add (8, "John")
add (15, "Joe")
So naturally I want to do something like a hashtable, but when I do a lookup, if it doesn’t find the exact value, I would like it to return the nearest key that isn’t greater than the requested key.
So for example, if I lookup 7, it should return “Sue”, but if I lookup 9, it should return “John”
I’m hoping to use one of the java util classes (HashTable, TreeMap, etc) but I’m not quite sure how to do it.
TreeMap from the collections library provides the functionality you’re looking for