I'd like to know what is there a better way to find the first value greater than an inputted value in a large SortedMap instead of looping through all values in my example below. Or if SortedMap is a the best structure to use for this.
Could this be achieved using google-collections? Thanks in advance
public class mapTest {
public static void main(String[] args) {
SortedMap<Double, Object> sortedMap = new TreeMap<Double, Object>();
sortedMap.put(30d, "lala");
sortedMap.put(10d, "foo");
sortedMap.put(25d, "bar");
System.out.println("result: " + findFirstValueGreaterThan(sortedMap, 28d));
}
public static Object findFirstValueGreaterThan(SortedMap<Double, Object> sortedMap, Double value) {
for (Entry<Double, Object> entry : sortedMap.entrySet()) {
if (entry.getKey() > value) {
// return first value with a key greater than the inputted value
return entry.getValue();
}
}
return null;
}
}