• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Correct implementation of hashcode()

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Whizlab SCJP1.4

public class Test{
public static void main(String args[]){
private int num;
public Test(int i){
num=i;
}
public int hashcode(){
int i=(int)(Math.random()*100);
return i*num;
}
}
}

a) code implements hashcode() correctly
b) code doesnot implements hashcode() correctly
c)This implementation is correct if and only if the equals() is not overridden
d) Given code doesnt contain enough information about correct implementation of hashcode()
e) None of these

According to me option b seems correct as for all objects created the hashcode() will return a unique value which is not as per the contract.

Kindly let me know if my answer is right or wrong? And if wrong please provide an explanation supporting your answer.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By "hashcode correctly", I am assuming you are referring to the hashcode contract used by the hashing collections?

If that is the case, the method is the hashCode() method. Java is case sensitive. The hashcode() method is not used by the hashing collections.

Henry
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, i am refering to the hashCode() method.
Sorry for the confusion caused due to typing error.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right. This is not the correct implementation of hashCode under current circumstances. this is because the equals method of Object class is not overriden, so the hashCode method may return different hash codes for multiple invocations on the same object....but the equals method of Object class will return true when called like this - obj.equals(obj);
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vidya,
if this is the correct coding, i agree with Ankith..
public class Test
{
public static void main(String args[]){}
private int num;
public Test(int i)
{
num=i;
}
public int hashCode()
{
int i=(int)(Math.random()*100);
return i*num;
}

}
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manju Kavi:
if this is the correct coding, i agree with Ankith..



why do people keep writing my wrong name. Is it that tough
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit & Manju.

Ankit, i just wanted to clarify another doubt raised from this question. As you said "The equals() method is not overridden". As per the contract of equals() and hashCode(), if we are overridding one we have to override the other.
But here since we are not overridding, i think we are refering to the Object class's equals() methods so in that case hashCode() will be returing unique hashcode values for objects that are equal or not equal too.

Kindly correct me if i am wrong.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But since the equals method is not overriden and the hashCode method uses Math.random, so it might return unequal hashCode for objects which are equal according to the equals method of the object class. The equals method of the object class checks for reference equality,

public boolean equals(Object other)
{
return this==other;
}

but the hashCode implementation here might return false on two references of same object. It might even return unequal hashCodes for the same reference. This will break the equals and hashCode contract...
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Ankit.
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm very sorry Ankit
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vidhya suvarna:
Source: Whizlab SCJP1.4

public class Test{
public static void main(String args[]){
private int num;
public Test(int i){
num=i;
}
public int hashcode(){
int i=(int)(Math.random()*100);
return i*num;
}
}
}

a) code implements hashcode() correctly
b) code doesnot implements hashcode() correctly
c)This implementation is correct if and only if the equals() is not overridden
d) Given code doesnt contain enough information about correct implementation of hashcode()
e) None of these

According to me option b seems correct as for all objects created the hashcode() will return a unique value which is not as per the contract.

Kindly let me know if my answer is right or wrong? And if wrong please provide an explanation supporting your answer.

 
Sampath Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"As per the contract of equals() and hashCode(), if we are overridding one we have to override the other". Is it a must condition?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manju Kavi:
I'm very sorry Ankit



I was just joking
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sampath..It is a good practice to overridde both.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Sampath Kumar:
"As per the contract of equals() and hashCode(), if we are overridding one we have to override the other". Is it a must condition?


Yes it is. Otherwise hash based data structures as java.util.HashMap and the like could be broken. If you only override the equals() method and use java.util.HashSet#contains() the result could be false, although there are several equal objects contained.

Regards,
Thomas
reply
    Bookmark Topic Watch Topic
  • New Topic