TreeMap.navigableKeySet().descendingIterator() sequence ?
martinrb at google.com
Sat Apr 19 08:37:49 UTC 2008
Yes, sad to say, looks like a serious and embarrassing bug.
This bit of code has no existing test case.
More tests should be added to MOAT.java.
Charlie, please file a bug.
In our defense, there are very many methods in the NavigableFoo interfaces,
and for completeness they have to be tested on subsets and submaps,
and Sun engineers have not historically had good code coverage tool support.
On Fri, Apr 18, 2008 at 2:39 PM, kevin bourrillion <kevinb at google.com>
> Wow. TreeMap.java line 1024:
> return new DescendingKeyIterator(getFirstEntry());
> Sure does look fishy.
> On Thu, Apr 17, 2008 at 2:54 PM, charlie hunt <charlie.hunt at sun.com>
> > Been looking at TreeMap's implementation rather closely and observed
> > something I don't understand. %-)
> > TreeMap (as of JDK 6 and later) implements NavigableMap. NavigableMap
> > requires an implementation of a navigableKeySet() method which returns a
> > NavigableSet<K>. So, in TreeMap I see:
> > public NavigableSet<K> navigableKeySet()
> > Then, the NavigableSet interface has an Iterator<E>
> > and Iterator<E>iterator(). The former returns an iterator over the
> > in descending order. The latter in ascending order.
> > I also noticed in TreeMap, NavigableMap requires an implementation of a
> > descendingKeySet(), i.e. public NavigableSet<K> descendingKeySet().
> > the NavigableSet interface has an Iterator<E> descendingIterator() and
> > iterator() as described above.
> > When I populate a TreeMap with some dummy data, let's say I populate it
> > with 15 Integer keys and String's representing those Integer keys as
> > I observe something I don't understand when using the
> > navigableKeySet().descendingIterator().
> > When, I try to iterate over the returned NavigableKeySet, I see the
> > key printed and that's it. Nothing else prints. From looking at the
> > for TreeMap and NavigableSet, this doesn't seem right. Shouldn't it
> > over the entire set in descending order? I looked at the source and it
> > creates an Iterator that starts at the lowest key and then tries to find
> > next lowest key (which obviously doesn't exist). Am I not reading
> > right in the JavaDoc?
> > When iterating using navigableKeySet().iterator() I see all 15 keys
> > in ascending order, (as expected).
> > When I iterate with descendingKeySet().descendingIterator() I see the
> > keys printed in ascending order, (as expected since descending set
> > by a descending iterator should give me ascending order).
> > When iterating using descendingKeySet().iterator, I see the 15 keys
> > in descending order, (as expected).
> > I've attached a very simple program that illustrates what I'm
> > Could someone clarify this behavior?
> > thanks,
> > charlie ...
> Kevin Bourrillion @ Google
> internal: go/javalibraries
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the core-libs-dev