JDK-6415641 : (coll) Getting NavigableMap/NavigableSet right
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-04-20
  • Updated: 2017-05-16
  • Resolved: 2006-05-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b86Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
NavigableSet and NavigableMap were added to Mustang to rectify deficiencies in SortedSet and SortedMap.  
Unfortunately, they did not quite solve the problems.  
There was no way to get a navigable descending view of a navigable collection.  
Further, there was no way to get any partial view other than a half-open ascending set or map.  
Getting another type of interval required horrible hackery that didn't work for some types, 
as per the SortedSet documentation:

--------------------------------------------------------------
Note: this method always returns a half-open range 
(which includes its low endpoint but not its high endpoint). 
If you need a closed range (which includes both endpoints), 
and the element type allows for calculation of the successor a given value, 
merely request the subrange from lowEndpoint to successor(highEndpoint). 
For example, suppose that s is a sorted set of strings. 
The following idiom obtains a view containing all of the strings in s from low to high, inclusive:

   SortedSet sub = s.subSet(low, high+"\0");
 

A similar technique can be used to generate an open range (which contains neither endpoint). 
The following idiom obtains a view containing all of the Strings in s from low to high, exclusive:

   SortedSet sub = s.subSet(low+"\0", high);
-------------------------------------------------------------

The solution to all of these problems is straightforward, and actually simplifies the interfaces:

In NavigableMap, we replace the method:

    Set<Map.Entry<K,V>> descendingEntrySet()

with a single method:

    NavigableMap<K,V> descendingMap();

Further, we take the three "partial view methods":

    NavigableMap<K,V> navigableHeadMap(K toKey);
    NavigableMap<K,V> navigableTailMap(K fromKey);
    NavigableMap<K,V> navigableSubMap(K fromKey, K toKey);

and add parameters to specify whether the endpoints are to be included in the returned view:

    NavigableMap<K,V> headMap(K toKey, boolean inclusive);
    NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
    NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
                             K toKey,   boolean toInclusive);

(the removal of the "navigable" prefix here makes for more usable names)

That's it for NavigableMap. The changes to NavigableSet are similar:

We add:

    NavigableSet<E> descendingSet();

Also, "inclusive" parameters are added to the three "partial view" methods:

    NavigableSet<E> headSet(E toElement, boolean inclusive);
    NavigableSet<E> tailSet(E fromElement, boolean inclusive);
    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                           E toElement,   boolean toInclusive);

The power and elegance of the interfaces are greatly enhanced. 

It is particularly important that we get these changes into Mustang, as these are *interfaces*.  
Once released, they cannot be changed.  
Moreover, they are replacements for existing interfaces (SortedSet and SortedMap done right).  
We will not get a third chance.

Comments
EVALUATION JSR166 Expert Group Alumni Doug Lea and Josh Bloch are providing a better API and implementation.
20-04-2006