Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4848853
Votes 8
Synopsis (coll) Modify LinkedHashMap and LinkedHashSet to include first() and last()
Category java:classes_util
Reported Against 1.4.1
Release Fixed
State 6-Fix Understood, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 15-APR-2003
Description
eldes 




A DESCRIPTION OF THE REQUEST :
In working with the LinkedHashMap class a bit recently, I think I've found a few additional convenience methods that could prove useful. The following is a list:

1. A method to get the first node in the list.  After a quick glance at the code, this seems like it would simply involve accessing the 'header' entry.
2. A method to get the last, or eldest entry in the list.  If I'm not mistaken, this seems like it would be the 'header.before' entry.
3. A method to support reverse iteration.  Since it is a doubly-linked list, once again, easy modification.
4. A method, given a key, to start iteration at that key's point in the list.

JUSTIFICATION :
1. Once again, if I'm correct, the only way to get the last (eldest) entry is to traverse the whole list via the iterator.
2. Reverse iteration would be even more complex.
3. Starting iteration given a particular key would involve a sequential search in traversing the list.
(Review ID: 184177) 
======================================================================
Work Around
N/A
Evaluation
All of fancy (ordered) maps and sets (TreeMap/Set, LinkedHashMap/Set) are somewhat lacking in "navigation ability."  It would be straightforward to add these operations, but somewhat in conflict with the Collection Framework's "interface-based" philosophy.  If there is sufficient demand, we may do it anyway.

  xxxxx@xxxxx   2003-06-19
It is worth investigating whether retrofitting
LinkedHashSet to implement the new Deque interface,
as was done with LinkedList, would be easy to do.
Posted Date : 2005-08-05 18:10:27.0
Comments
  
  Include a link with my name & email   

Submitted On 27-JUN-2003
toddwright
Wait, scratch that first comment -- it's already ordered
with the eldest at the front, so you can use the iterator
as-is for most LRU applications.

A reverse iterator would allow the client to easily list the
most-popular entries (or, if in insertion order, the most
recent insertions).


Submitted On 27-JUN-2003
toddwright
One argument for the reverse iterator is that it allows the
LinkedHashMap's "removeEldestEntry(..)" method to remove one
or more old entries, instead of just the eldest.   For
example, we may want to remove the eldest half of the
entries, or we really want to remove the second-eldest entry
(e.g. in a cache with a few non-evictable entries).  To do
this now requires a copy of the values into a list/array
that supports reverse iteration.

I see the API philosophy conflict.  If you follow the List
convention, this would require a new "listIterator()"
method, or a LinkedList view of the "values()".  At worst
these could be hidden from the interface, similar to
ArrayList's "trimToSize()".


Submitted On 11-JUL-2003
Osen
And a method to insert an entry at a given position between 
the existing entries would also be useful. Currently there's 
only the put-method which inserts at the end.


Submitted On 03-JAN-2005
theshade
I


Submitted On 03-JAN-2005
theshade
Comment got lost when I logged in... again: I'd find a reverse iterator useful as well.  I'm using a LinkedHashSet to keep track of visited items (highlighted in a different color).  I want to use the set to display the last X visited items.  With a doubly linked list this would be trivial, but there's no such method in LinkedHashSet.  The speed of contains() speed is important as the set grows very large.


Submitted On 07-AUG-2005
barryg2004
I'd also like to see the Set returned by LinkedHashMap.keySet implement the "java.uil.SortedSet" interface.  At present the type returned is: HashMap.KeySet.  i.e. the iteration order is implied rather than guaranteed.


Submitted On 10-DEC-2007
I would prefer to iterate the list both insertion order (as currently) and reverse insertion order, but lacking that, it would be nice, at the very least, to specify iteration order on construction as insertion (like today) or reverse insertion order. 


Submitted On 04-JUN-2009
It would be very useful to be possible to iterate starting with an arbitrary element.


Submitted On 04-JUN-2009
It would be very useful to be possible to iterate starting with an arbitrary element.


Submitted On 26-OCT-2009
msenesi
I would absolutely appreciate these features.



PLEASE NOTE: JDK6 is formerly known as Project Mustang