Вопрос

Let's say I have a TreeMultimap (from the Google Guava libraries) with pairs of keys/values. If I have a key (assume keys are of type int), how would I go about getting the values of both that key and the nearest key?

So for example, my treemultimap is:

1 -> "Hello"

3 -> "Hola"

5 -> "Bonjour"

My key is 5. I want to retrieve the nearest key (which would be 3). I could then use that key to retrieve the nearest value.

It seems like this should be a simple matter of navigating to the possessed key and then iterating to the next one, but I can't find any functionality with TreeMultimap which would allow me to do this (ie. there is no function that gives me access to the nodes themselves, I can allow retrieve collections of keys and iterate through them in linear fashion, which defeats the purpose of having a sorted tree). I feel like there's a simple solution to this that I'm missing. ANy help would be greatly appreciated!

Это было полезно?

Решение

TreeMultimap.keySet() returns a NavigableSet. So, for example, the next-lower key to k can be retrieved with TreeMultimap.keySet().lower(k).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top