equals() and hashcode() not working...

E

e.chaitanya

Hi all

We are trying to override the equals() and hashCode() methods for a
particular class. Given below is a piece of code we have tested:

import java.util.*;

public class SetCompare {

private int val;
private String data = "TestString";

public SetCompare(int val)
{
this.val=val;
}

public int hashCode()
{
System.out.println("Hashed! :"+val);
int hash = 7;
hash = 31 * hash + val;
hash = 31 * hash + (null == data ? 0 : data.hashCode());
System.out.println("Hashed value :"+hash);
return hash;
}


public boolean equals(Object obj)
{
System.out.println("\n *************");
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
// object must be Test at this point
SetCompare test = (SetCompare)obj;
return val == test.val &&
(data == test.data || (data != null && data.equals(test.data)));
/*if(!(a1 instanceof SetCompare))
return false;

if(this.val==((SetCompare)a1).val)
return true;
else
return false;*/
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set newSet = new HashSet();
SetCompare obj1 = new SetCompare(1);
SetCompare obj2 = new SetCompare(2);


newSet.add(obj1);
newSet.add(obj2);

if (newSet.contains(obj1))
System.out.println("\n Test");


}

}

However, this piece of code does not call the equals() method when the
"newSet.contains(obj1)" or "newSet.add(obj2)" is called. Could any of
you suggest a fix for this and also help us identify the problem with
it.

Thank You

---
 
P

Patricia Shanahan

However, this piece of code does not call the equals() method when the
"newSet.contains(obj1)" or "newSet.add(obj2)" is called. Could any of
you suggest a fix for this and also help us identify the problem with
it.
....

The Object contract for hashCode and equals allows HashSet to assume
both that two objects with different hash codes are unequal, and that
every object is equal to itself. All the comparisons it needs to do in
your test program are covered by one of those rules.

If you want to force a call to equals, probe with an object that is
equal, but not identical, to one of the objects in the HashSet:

if (newSet.contains(new SetCompare(1)))
System.out.println("\n Test");

Patricia
 
H

hiwa

Try this and see source code for HashSet and HashMap.
--------------------------------------------------
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<SetCompare> newSet = new HashSet<SetCompare>();
SetCompare obj1 = new SetCompare(1);
SetCompare obj2 = new SetCompare(2);
SetCompare obj3 = new SetCompare(1);

newSet.add(obj1);
newSet.add(obj2);

if (newSet.contains(obj1)){
System.out.println("Ya, contains obj1");
}
System.out.println("-------------------");
if (newSet.contains(obj3)){
System.out.println("Ya, contains obj3");
}
}
}
 
A

Alex Hunsley

Hi all

We are trying to override the equals() and hashCode() methods for a
particular class. Given below is a piece of code we have tested:
import java.util.*;

public class SetCompare {

A little note on style:
Avoid class names that sound like verbs (actions) are their main jist.
Use noun (thing) class names. You can still suggest a verb by using a
noun though:
in your example, I would name the class "ComparableSet".

If I had better linguistics knowledge I'm sure I'd know more exact (and
confusing?) terminology here!
 
A

Alex Hunsley

Alex said:
A little note on style:
Avoid class names that sound like verbs (actions) are their main jist.
Use noun (thing) class names. You can still suggest a verb by using a
noun though:

Sorry, should read "you can still put a verb in front of a noun"...
 
O

Oliver Wong

Alex Hunsley said:
Sorry, should read "you can still put a verb in front of a noun"...

"Comparable" in "ComparableSet" is an adjective. If you put a verb in
front of a noun, it'll sound like a verb phrase, with the object of the verb
being the noun, e.g. "CompareSet".

- Oliver
 
A

Alex Hunsley

Oliver said:
"Comparable" in "ComparableSet" is an adjective. If you put a verb in
front of a noun, it'll sound like a verb phrase, with the object of the
verb being the noun, e.g. "CompareSet".

Oops, you're right. I am confusing verbs and adjectives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top