-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.4.1
-
x86
-
windows_2000
Name: jl125535 Date: 04/27/2004
FULL PRODUCT VERSION :
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)
A DESCRIPTION OF THE PROBLEM :
The Collection that HashMap returns from HashMap.values() does not override equals() and thus returns incorrect results when it is compared to another Collection with equals().
It appears this can be fixed by simply overriding equals() in the HashMap.Values class.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the given test case.
While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface "directly" (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals. It is not necessary to do so, and the simplest course of action is to rely on Object's implementation, but the implementer may wish to implement a "value comparison" in place of the default "reference comparison." (The List and Set interfaces mandate such value comparisons.)
The general contract for the Object.equals method states that equals must be symmetric (in other words, a.equals(b) if and only if b.equals(a)). The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. (By the same logic, it is not possible to write a class that correctly implements both the Set and List interfaces.)
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
trueThe collection returned by the values() view is neither a set nor a list, and cannot usefully override equals.
true
ACTUAL -
true
false
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class Test {
public static void main (String args[]) {
java.util.HashMap one= new java.util.HashMap();
java.util.HashMap another= new java.util.HashMap();
one.put("true", Boolean.TRUE);
one.put("false", Boolean.FALSE);
another.put("true", Boolean.TRUE);
another.put("false", Boolean.FALSE);
System.out.println(one.equals(another)); // prints true as expected
// prints false even though the values are indeed equal:
System.out.println(one.values().equals(another.values()));
}
}
---------- END SOURCE ----------
(Incident Review ID: 214985)
======================================================================