Hashcode and Equal

J

Javas

import java.util.*;
public class Qn353
{
private String name;
public Qn353(String name)
{
this.name = name;
}

public boolean equals(Object o)
{
if ( ! (o instanceof Qn353) ) return false;
Qn353 p = (Qn353) o;
return p.name.equals(this.name);
}
public static void main(String [] args)
{
HashSet<Object> hs = new HashSet<Object>();
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
}
public int hashcode()
{
return 1;
}



}


Above code outputs : 2
But I thought it is supossed to be 1 since the hashcode is the same
for both the objects, equals also holds true and the set doesnt allow
duplicates. What is wrong? Please correct me.

Thanks!
 
R

RedGrittyBrick

import java.util.*;
public class Qn353
{
private String name;
public Qn353(String name)
{
this.name = name;
}

public boolean equals(Object o)
{
if ( ! (o instanceof Qn353) ) return false;
Qn353 p = (Qn353) o;
return p.name.equals(this.name);
}
public static void main(String [] args)
{
HashSet<Object> hs = new HashSet<Object>();
hs.add(p1);
hs.add(p2);

Where did p1 and p2 spring from? Does this compile? I'd expect the Java
compiler to be a bit puzzled about the nature of p1 and p2 at this point.

System.out.println(hs.size());
}
public int hashcode()
{
return 1;
}



}


Above code outputs : 2

Some other code might but I don't see how this code can run.

But I thought it is supossed to be 1 since the hashcode is the same
for both the objects, equals also holds true and the set doesnt allow
duplicates. What is wrong? Please correct me.

Maybe you're assuming p1 and p2 are instances of Qn353 when they are not.
 
R

Roedy Green

Above code outputs : 2
But I thought it is supossed to be 1 since the hashcode is the same
for both the objects, equals also holds true and the set doesnt allow
duplicates. What is wrong? Please correct me.

The hashCode has nothing whatever to do with what is considered a
duplicate. Setting the hashCode to 1 just destroys the efficiency of
the HashCode lookup.

Keep in mind that even in ordinary use, HashCodes are not unique, and
even less after having the high bits trimmed off to index the lookup
table.

see http://mindprod.com/jgloss/hashcode.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
~ Tom Cargill
 
R

Roedy Green

import java.util.*;
public class Qn353
{
private String name;
public Qn353(String name)
{
this.name = name;
}

public boolean equals(Object o)
{
if ( ! (o instanceof Qn353) ) return false;
Qn353 p = (Qn353) o;
return p.name.equals(this.name);
}
public static void main(String [] args)
{
HashSet<Object> hs = new HashSet<Object>();
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
}
public int hashcode()
{
return 1;
}

This would not compile. You never define p1 and p2.
--
Roedy Green Canadian Mind Products
http://mindprod.com

The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
~ Tom Cargill
 
A

Andreas Leitgeb

Javas said:
public int hashcode()
{
return 1;
}

Defining a method hashcode() has no effect on Collections at all,
because they call hashCode() (with capital C), instead.

The other comments (p1 and p2 undefined, and the actual effect of
overriding correctly spelled hashCode() to return a constant)
also apply.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top