Re: Glitch in Java Collections (No descendingMap in LinkedHashMap)
On 05.10.2012 20:05, Lew wrote:
Robert Klemme wrote:
Lew wrote:
Robert Klemme wrote:
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.
Lew, I'm sorry, but I think you're overly picky here.
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)."
Well, the entrySet() obtained for LHM does provide this guarantee. Even
though it's not explicitly stated in comments for methods entrySet(),
keySet() and values() because they are inherited the class level comment
makes it clear that the ordering observed during iteration will usually
be insertion order. I would conceded that the fact that you can also
have access order if invoking one specific constructor is a not made
explicit enough in the class JavaDoc.
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.
I think that clearly derives from the class comment because there is no
other way to iterate a Map than through the three dependent collections
(which usually include this comment in their accessor methods: "The
collection is backed by the map, so changes to the map are reflected in
the collection, and vice-versa.").
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.
And?
Regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/