-
Enhancement
-
Resolution: Fixed
-
P2
-
6
-
b86
-
generic
-
generic
-
Verified
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.
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.
- relates to
-
JDK-6377302 AbstractCollection.toArray is not safe for concurrent collections
- Closed
-
JDK-4155650 (coll) Sorted Collection Classes with bidirectional navigation
- Resolved
-
JDK-4912961 (coll) The set returned by TreeMap.keySet should implement SortedSet
- Closed
-
JDK-5045147 (coll) Adding null key to empty TreeMap without Comparator should throw NPE
- Closed
-
JDK-7129185 (coll) Please add Collections.emptyNavigableSet()
- Closed