i have a class, in which i have override the Object.equals(Object o)
method... and then i try to make two equal objects (under the new
definition) and place them into a HashSet... which should only accept one
of them, as in the following code. the class holds two objects (a pair of
objects) and if another member of the class contains the same two objects
(in either place of the ordered pair), it is considered equal.
Integer key1 = new Integer(1);
Integer key2 = new Integer(2);
HashSet x = new HashSet();
// demonstrate that the equals() method works...
System.out.println((new MyClass(key1, key2)).equals(new myClass(key2,
key1)));
System.out.println(x.add(new MyClass(key1, key2)));
System.out.println(x.add(new MyClass(key2, key1)));
if i'm correct in reading the docs about how Set classes work, the output
of this should be:
true
true
false
but instead i'm getting:
true
true
true
and indeed the Set x has two elements after this code.
i thought the Set code checks for membership by applying the .equals()
method, which at runtime should bind to my overridden method, no?
thanks for any help,
murat
--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu