JDK-4848853 : (coll) Modify LinkedHashMap and LinkedHashSet to include first() and last()
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2003-04-15
  • Updated: 2025-02-05
Related Reports
Duplicate :  
Duplicate :  
Description
eldes 

Name: rmT116609			Date: 04/15/2003


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) 
======================================================================

Comments
Most of this is implemented by Sequenced Collections (JDK-8266571). The only remaining request here is (4) the ability to iterate starting from a particular element. In principle this doesn't seem too difficult; a LinkedHashIterator (internal class) could be constructed from a particular node instead of starting either at the beginning or the end. This seems straightforward enough for LinkedHashSet. Manifesting this in the API for LinkedHashMap is a bit tricky. Iterating over LinkedHashMap occurs only on the views (keys, values, entries) and not on the Map itself. Continuing the iteration, possibly with removals, is straightforward. However, the immediate follow-on request will be to insert a value before or after the current element or mapping, or to reorder an existing mapping into the current position. (Updating the current mapping can be accomplished by calling setValue() on a returned Entry object.) This might require the addition of a "LinkedHashMapIterator" to accommodate the new operations; or ListIterator could be pressed into service for this, at the cost of some contortions. As usual, the value here is a bit questionable, and it's not clear whether it's worth making a bunch of API changes for this. In the SequencedCollection design, these sorts of functions were considered but omitted because they seemed to require a large API footprint compared to the amount of functionality they offered.
05-02-2025

EVALUATION It is worth investigating whether retrofitting LinkedHashSet to implement the new Deque interface, as was done with LinkedList, would be easy to do.
05-08-2005

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. ###@###.### 2003-06-19
19-06-2003