JDK-4216399 : (coll) TreeMap: please make getCeilEntry and getPreviousEntry >>public<<
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 1999-03-02
  • Updated: 2012-10-08
  • Resolved: 2005-09-04
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 b51Fixed
Description
Name: dbT83986			Date: 03/02/99


I needed a method to find the next or previous in a TreeMap if
the entry isn't in the collection. There already is a private 
method that does exactly that, but I have no clue why it is 
private. Pretty please make this public (not that i will wait 
for it to become public, but it would be good for all Those 
To Come)

ps: good luck with Java
(Review ID: 52814)
======================================================================

Comments
EVALUATION This is a known deficiency in TreeMap (the SortedMap interface actually), but there are satisfactory workarounds (described in the workaround field of this bug report). We thought hard about this when we designed the interface and made a conscious decision not to export these methods in the interface. The main reasons for the decision (to the best of my recollection) are: 1) The methods would be, in effect, an alternative form of iteration. Currently, *ALL* iteration over Maps is done via "Set-views". We were loath to give up the consistency. 2) There are *three* iterable Set views: keys, values and entries. Thus, adopting the suggestion would have added six rather than two calls to the Map API (or left obvious holes in it). Consistency would have forced us to add six analogous methods to TreeSet. Twelve new calls in the core collection interfaces seemed excessive. 3) It would have raised the issue of what to do when there is no next (or previous) entry. Because null is a legitimate key/entry value, we would have pretty much been forced to throw a NoSuchElement exception. We were afraid that people would have used the new methods to iterate over Sorted Sets/Maps, terminating the iteration by catching the exception. This would be unacceptable from a performance and style point of view: throwing exceptions is very expensive, and using (unchecked!) exceptions for normal control flow is very much frowned upon. joshua.bloch@Eng 1999-03-08 As time passes it becomes clearer that we should find some way to provide this functionality. ###@###.### 2004-01-19 A fix for this will be provided as part of JSR166 maintenance. ###@###.### 2005-03-09 03:02:30 GMT
19-01-2004

WORK AROUND To get the predecessor of a given key (k) in a SortedMap m: Obejct pred = m.headSet(k).lastKey(); To get the successor: a) If you know that the Map *doesn't* contain an entry for k: Object succ = m.tailSet(k).firstKey(); b) If m might contain an entry for k: Iterator i = m.tailSet(k); Object succ = i.next(); if (succ.equals(k)) succ = i.next(); There are many related alternatives. For example, these idioms can be tweaked to give values in addition to keys. See the SortedSet section of the collections tutorial ( http://java.sun.com/docs/books/tutorial/collections/interfaces/sorted-set.html ) for related information. joshua.bloch@Eng 1999-03-08
08-03-1999