Note that there are some explanatory texts on larger screens.

plurals
  1. POAccesing hidden getEntry(Object key) in HashMap
    text
    copied!<p>I have similar problem to one discussed <a href="https://stackoverflow.com/questions/1873330/why-is-getentryobject-key-not-exposed-on-hashmap">here</a>, but with stronger practical usage.</p> <p>For example, I have a <code>Map&lt;String, Integer&gt;</code>, and I have some function, which is given a key and in case the mapped integer value is negative, puts <code>NULL</code> to the map:</p> <pre><code>Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;(); public void nullifyIfNegative(String key) { Integer value = map.get(key); if (value != null &amp;&amp; value.intValue() &lt; 0) { map.put(key, null); } } </code></pre> <p>I this case, the lookup (and hence, <code>hashCode</code> calculation for the key) is done twice: one for lookup and one for replacement. It would be nice to have another method (which is already in <code>HashMap</code>) and allows to make this more effective:</p> <pre><code>public void nullifyIfNegative(String key) { Map.Entry&lt;String, Integer&gt; entry = map.getEntry(key); if (entry != null &amp;&amp; entry.getValue().intValue() &lt; 0) { entry.setValue(null); } } </code></pre> <p>The same concerns cases, when you want to manipulate immutable objects, which can be map values:</p> <ul> <li><code>Map&lt;String, String&gt;</code>: I want to append something to the string value.</li> <li><code>Map&lt;String, int[]&gt;</code>: I want to insert a number into the array.</li> </ul> <p>So the case is quite common. Solutions, which might work, but not for me:</p> <ul> <li>Reflection. Is good, but I cannot sacrifice performance just for this nice feature.</li> <li>Use <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/AbstractHashedMap.html#getEntry%28java.lang.Object%29" rel="nofollow noreferrer"><code>org.apache.commons.collections.map.AbstractHashedMap</code></a> (it has at least <code>protected getEntry()</code> method), but unfortunately, commons-collections do not support generics.</li> <li>Use <a href="http://collections.sourceforge.net/api/org/apache/commons/collections/map/AbstractHashedMap.html#getEntry%28java.lang.Object%29" rel="nofollow noreferrer">generic commons-collections</a>, but this library (AFAIK) is out-of-date (not in sync with latest library version from Apache), and (what is critical) is not available in central maven repository.</li> <li>Use value wrappers, which means "making values mutable" (e.g. use mutable integers [e.g. <code>org.apache.commons.lang.mutable.MutableInt</code>], or collections instead of arrays). This solutions leads to memory loss, which I would like to avoid.</li> <li>Try to extend <code>java.util.HashMap</code> with custom class implementation (which should be in <code>java.util</code> package) and put it to <a href="http://java.sun.com/j2se/1.5.0/docs/guide/standards/index.html" rel="nofollow noreferrer">endorsed folder</a> (as <code>java.lang.ClassLoader</code> will refuse to load it in <code>Class&lt;?&gt; defineClass(String name, byte[] b, int off, int len)</code>, see sources), but I don't want to patch JDK and it seems like the list of packages that can be endorsed, does not include <code>java.util</code>.</li> </ul> <p>The similar question is already raised on <a href="http://bugs.sun.com/view_bug.do?bug_id=6552529" rel="nofollow noreferrer">sun.com bugtracker</a>, but I would like to know, what is the opinion of the community and what can be the way out taking in mind the maximum memory &amp; performance effectiveness.</p> <p>If you agree, this is nice and beneficiary functionality, please, vote this bug!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload