override equals and hashcode
This is a multi-part message in MIME format.
------=_NextPart_000_0009_01C7DAD2.B9EA0EE0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
I have a class Obj. I created three objects o1,o2,03. Objects are equal =
if their id is equal. For example, o1 and o3 are equal. I tried adding =
these three objects to a HashSet, and when I print the size of the =
hashset I get "3" and not "2". Something is not right with the equals or =
hashcode method. Can someone shed some light?
code snippet:
import java.util.*;
public class Test
{
public static void main (String[] args)
{
Set hs = new HashSet();
Obj o1 = new Obj ("w", 10);
Obj o2 = new Obj ("r", 20);
Obj o3 = new Obj ("w", 30);
hs.add(o1);
hs.add(o2);
hs.add(o3);
System.out.println(hs.size());
}
public static class Obj
{
String id;
int length;
Obj (String id, int length)
{
this.id = id;
this.length = length;
}
boolean equals (Obj o)
{
if (o == null)
return false;
else if (o instanceof Obj)
return (this.id.equals(o.id));
else return false;
}
public int hashCode()
{
return id.hashCode();
}
}
}
------=_NextPart_000_0009_01C7DAD2.B9EA0EE0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; =
charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16481" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Verdana size=2>Hi,</FONT></DIV>
<DIV><FONT face=Verdana size=2></FONT> </DIV>
<DIV><FONT face=Verdana size=2>I have a class Obj. I created three =
objects
o1,o2,03. Objects are equal if their id is equal. For example, o1 and o3 =
are
equal. I tried adding these three objects to a HashSet, and when I print =
the
size of the hashset I get "3" and not "2". Something is not right with =
the
equals or hashcode method. Can someone shed some light?</FONT></DIV>
<DIV><FONT face=Verdana size=2></FONT> </DIV>
<DIV><FONT face=Verdana size=2>code snippet:</FONT></DIV>
<DIV><FONT face=Verdana size=2></FONT> </DIV>
<DIV><FONT face=Courier size=2>import java.util.*;</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier size=2>public class =
Test<BR>{<BR> public static
void main (String[] args)<BR> {<BR> Set hs = new
HashSet();<BR> Obj o1 = new Obj ("w", =
10);<BR> Obj o2 =
new Obj ("r", 20);<BR> Obj o3 = new Obj ("w",
30);<BR> <BR> hs.add(o1);<BR> hs.add(o2)=
;<BR> hs.add(o3);<BR> <BR> System.out.pr=
intln(hs.size());<BR> }<BR> <BR> public
static class Obj<BR> {<BR> String id;<BR> int =
length;<BR> <BR> Obj (String id, int
length)<BR> {<BR> this.id =
id;<BR> this.length =
length;<BR> }<BR> <BR> boolean equals =
(Obj
o)<BR> {<BR> <BR> if (o =
==
null)<BR> return false;<BR> else =
if (o
instanceof Obj)<BR> return
(this.id.equals(o.id));<BR> else return
false; <BR> }<BR> <BR> public int
hashCode()<BR> {<BR> return
id.hashCode();<BR> }<BR> }<BR>}</FONT></DIV></BODY></HTML=
------=_NextPart_000_0009_01C7DAD2.B9EA0EE0--