I would like to know that there is a better way to find the first value more than the entered value in the large SortedMap, and not iterate over all the values in my example below. Or if SortedMap is the best framework to use.
Can this be done with 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 entry.getValue();
}
}
return null;
}
}
source
share