A HashMap bug with a Proxy value?

David M. Lloyd david.lloyd at redhat.com
Wed Jan 12 16:56:11 UTC 2011

In my experience, the following are true:

1. Usage of any object has a hash key whose equals() result depends on
external or mutable factors should never be used as a hash key for any
2. Any object class for which, when given two instances "a" and "b", a
== b is true but a.equals(b) is false is incorrectly implementing .equals().
3. Any object whose equals() or hashCode() method implementation
performs network activity (e.g. a DNS request) is defective by design.

This includes java.net.URL as well as java.net.Proxy and any other JDK
classes which have these unfortunate design errors.

On 01/12/2011 10:43 AM, Chris Hegarty wrote:
> Are you referring to java.net.Proxy? Proxy.equals depends on the
> resolution of its address. Address resolution could change over time,
> depending on the caching policy. java.net.URL has a similar issue.
> Strangely,I would have expected containsValue and containsKey to behave
> in a similar fashion. I think the specification for these methods is
> very clear, they should use equals(). I'm not sure if containsKey is
> correct if it accepts key equality.
> -Chris
> On 01/12/11 03:53 PM, Jing LV wrote:
>> Hello everyone,
>> (As I am new in OpenJDK, please tell me if I post on the wrong
>> mailing-list).
>> I find an odd behaviour of HashMap. If we put a value which is an
>> instance of Proxy, it would return false while checking
>> containsValue(proxyinstance). I checked use Proxy instance as a key, it
>> works well.
>> I suppose this is a bug of HashMap. By reading the code closely, I find
>> the problem was that, for Proxy instance, Proxy.equals(Proxy) may return
>> false (there are many discussions on this already).
>> HashMap.containsKey() will check if there is a key in the map equals
>> and/or the same with the given key(so it bypass equals check) with
>> getEntry() method but HashMap.containsValue() does not. So a quick fix
>> will be: check there is a value in the map that equals and/or the same
>> with the given value.
>> I'd like to report this bug to bugzilla of openjdk, with my testcase and
>> patch. Please tell me if any comments. Thanks a lot.
>> Best wishes!
>> Jing LV


More information about the core-libs-dev mailing list