Re: Glitch in Java Collections (No descendingMap in LinkedHashMap)
Robert Klemme wrote:
Lew wrote:
Robert Klemme wrote:
Jan Burse wrote:
You get insertion order how?
First paragraph of JavaDoc:
Hash table and linked list implementation of the Map interface, with
predictable iteration order. This implementation differs from HashMap in
that it maintains a doubly-linked list running through all of its
entries. This linked list defines the iteration ordering, which is
normally the order in which keys were inserted into the map
(insertion-order). Note that insertion order is not affected if a key is
re-inserted into the map.
That says how you put the order, not how you get the order.
The interesting thing about this class is that it has no public methods that reveal
the order.
Why would they need to?
If the ordered state is not observable, what difference does it make?
So what difference does the order make?
There are some use cases described in the class JavaDoc.
It doesn't explain how you reveal the order, only how you put it into another structure.
I can guess, but the docs don't confirm, that iterators off the keyset or entryset would
respect the order, but the documentation for the 'keySet()' and 'entrySet()' methods doesn't
promise this.
You do not have to guess - logical reasoning is enough. The only way to
iterate through a Map is via entrySet(), keySet() and values(). What
other methods could make use of the order if not these?
But these methods do not promise to return the data in any particular order, particularly
they do not promise to return the data in the order stored. In fact, they promise not to,
necessarily. From Set#iterator(): "The elements are returned in no particular order
(unless this set is an instance of some class that provides a guarantee)."
The Map makes the guarantee, and one has to infer the underlying Set will therefore
make that guarantee, but when one copies the Map into another Map implementation,
even that implicit promise is removed. Furthermore, why do they not state that the Set
implementation iterator's order is guaranteed? It seems there's a hole in the docs.
It's useful for subclasses, from what the documentation hints, but to the public?
It seems you could create a LRU Map with this, right. Other uses are
described in the class doc as mentioned above.
Right, that LRU Map would be a subclass, as I said.
--
Lew