Re: Glitch in Java Collections (No descendingMap in LinkedHashMap)
Robert Klemme wrote:
Jan Burse wrote:
I just wonder whether I have overlooked something.
But could it be that LinkedHashMap is lacking the
following API:
Map<K,V> descendingMap();
It's also lacking 'ascendingMap()'
So I can only iterate from oldest to youngest, and
not the other direction?
I guess they omitted it because the purpose is to provide a Map which
always returns elements in insertion order (and not reverse insertion
order).
Although under the hood it uses a double linked list,
and a reverse iterator would be simple.
Also the whole matter seems to be complicated, since
the other collections have descendingMap() in an
Interface NavigableMap, which extends SortedMap.
There you see why it's not in LinkedHashMap: it's neither a NavigableMap
nor a SortedMap.
But SortedMap map assumes a compartor, whereby the
sorting in LinkedHashMap is implicit.
There is no sorting going on - you just get insertion order.
You get insertion order how?
The interesting thing about this class is that it has no public methods that reveal
the order.
So what difference does the order make?
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.
It's useful for subclasses, from what the documentation hints, but to the public?
--
Lew