【发布时间】:2017-09-18 02:41:22
【问题描述】:
我有一个排序的地图,我希望找到最接近的“之前”和“之后”条目的某些键,这些条目可以完全满足某些条件。
Entry findAfter(TreeMap map, Key key, Predicate p) {
Entry e = map.higherEntry(key);
while (e!=null && !p.test(e.getValue())
e = map.higherEntry(e.getKey());
return e;
}
这似乎效率低下,因为 O(logN) 中的更高条目。有没有更好的办法?
我希望 TreeMap 有可以在 O(1) 中工作的 map.nextEntry(e),但据我所知,没有。
【问题讨论】: