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: 6552529
Votes 3
Synopsis (coll) Map.getEntry(key), Map.getKey(key)
Category java:classes_util
Reported Against
Release Fixed
State 5-Cause Known, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 02-MAY-2007
Description
A DESCRIPTION OF THE REQUEST :
I think the only reason this hasn't been implemented before is because it is Too Obvious, so no-one saw the need.

I would like to see public getEntry(Object key) and getKey(Object key) methods added to the Map interface, or at least to the WeakHashMap class.

JUSTIFICATION :
This would be useful because it would allow the application to get hold of the actual  customer  used as the key in the Map given an  customer  which is just equivalent. For a WeakHashMap this can be important, because it provides a way of ensuring that entries do not disappear from the map before the application has completely finished with them, by making everything use the same instance of the key  customer .

Moreover, it allows a slightly faster method of updating the value of  a mapping. A common code pattern is to use a Map to store instance counts or some similar status information for multiple "things", which needs to be updated multiple times. The normal way to do this is to call Map.get with the "thing", take the value returned, and then call Map.put with an altered value, resulting in two map lookup operations. If a getEntry() method is available, then a single lookup operation can retrieve the Entry, and the setValue method of the Entry can then be used to update the value without having to perform another lookup operation.


CUSTOMER SUBMITTED WORKAROUND :
Currently I have to use a second WeakHashMap mapping from the key objects to a WeakReference holding the key. This allows me to disambiguate the key objects, but it does not provide access to the Entry objects.
Posted Date : 2007-05-02 00:48:22.0
Work Around
N/A
Evaluation
An interesting suggestion.
Unlikely to happen, since methods can never be 
added to Interfaces compatibly.

As a practical matter, WeakHashMaps should only be used to store
objects with a trivial .equals() method, since the garbage collector
uses identity semantics, not value equality.
Posted Date : 2007-05-02 01:09:13.0
Comments
  
  Include a link with my name & email   

Submitted On 14-NOV-2007
NavigableMap was added to address the shortcomings of SortedMap. I don't see why Map couldn't have an EntrySet (or however named) subinterface to expose this valuable functionality.


Submitted On 16-JUL-2008
david.connard
Regarding the comment "unlikely to happen" - the method doesn't need to be added to the Map Interface, although I don't see why that has to be a blocking issue, if you also added a reference implementation to the AbstractMap class.  Virtually all implementations of the Map interface would start by extending from AbstractMap, don't they?

However, more importantly, it doesn't really have to be added to the base interface.  Yes, there is a performance argument for making this change higher up the class heirachy, but that's not the core reason for requesting this change.  The core reason is specific to WeakHashMap usages alone, and this change request would be satifisfied purely by making the existing WeakHashMap.getEntry(Object key) method public, rather than package protected.  That's just the addition of "public " to line 368 of WeakHashMap.java...  can we have it, please....?!

Allowing direct access to the keys in a WeakHashMap would allow you to share the referents of the keys around.  This is critical if you want to be able to hand out a new strong reference to something in your map.  You would do this if you are trying to share an identical object between various user sessions, yet have that object garbage collected once no sessions remain which refer to it.

The code flow for something like this would be:

* The first session calls a utility class to check the WeakHashMap, finds no key, creates an object, stores it weakly in the map, and returns a strong reference to be stored in the first session.

* The second session calls the utility class for the same key.  This checks the map, finds the weak key, and returns the strong reference to the existing key, to be stored in the second session.

(and so on, with more sessions...)

Then, all sessions share the one object instance in that particular case, until no further sessions remain, and it can be safely garbage collected, as the only remaining references are weak ones in the WeakHashMap.

There is currently no way to do achieve this strong reference re-use without an expensive O(n) iteration over the entire Map.keySet().  Yet, WeakHashMap already has the required method implemented, it's just not public.

Don't dismiss this just because you don't want to change the Map interface... if not, that's fine, but at least change this one method to be public instead.

Thanks!



PLEASE NOTE: JDK6 is formerly known as Project Mustang