On Mon, Nov 5, 2012 at 7:27 PM, Eric Peters <
ericp...@gmail.com> wrote:
> I was porting over some java code that was using java.util.TreeMap and
> wouldn't quite replicate the same behavior in the scala immutable TreeMap.
>
> One method that was missing from the scala TreeMap is: getHigherEntry(K
> key): "Gets the entry for the least key greater than the specified key",
> should be simple to implement.
>
> However, I can't seem to find any methods in the TreeMap that won't recurse
> through the whole map! I would expect that in a TreeMap, I would be able to
> search for a key using a compare method of what's stored in the tree and
> just look through the minimum number of branches to reach the leaf. Instead
> I'm finding it searching all of the branches.
I'm not sure why you think "find" should be able to understand that
your predicate is related to how the tree is ordered, but, to put it
simply, it doesn't. It derives no information from the result of the
predicate other than that's not the element desired, so it needs to
keep looking.
Moreover, find will return the *first* element, according to the
tree's order, so it has to start from the least element and test one
by one from there.
There's really no such method on Scala's TreeMap. You can use "from"
instead, but before Scala 2.10 it is just as inefficient as using
find.
--
Daniel C. Sobral
I travel to the future all the time.