Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on hascode()

 
velan vel
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai ranchers
can any one explain about this question


import java.util.*;
public class Info
{
String s1, s2, s3;
public Info(String a, String b, String c)
{
s1=a; s2=b; s3=c;
}
public boolean equals(Object obj)
{
if(! (obj instanceof Info) ) return false;
Info i = (Info) obj;
return (s1+s2+s3).equals(i.s1+i.s2+i.s3);
}
public int hashCode()
{
return s1.hashCode();
}
public static void main(String[] args)
{
HashMap map = new HashMap();
Info i1 = new Info("aaa", "aaa", "aaa");
Info i2 = new Info("aaa", "bbb", "ccc");
map.put(i1, "hello"); //1
map.put(i2, "world"); //2
}
}

------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
 
Mark Dathorne
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the question is?
 
velan vel
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the question is


Which of the following statements are correct regarding the above class?

Select 1 correct option.
a This is an invalid implementation of hashCode() method with respect to the given equals() method.


b Only one of the Info objects will be stored in the HashMap.


c Both the objects will be stored in the HashMap.


d An exception will be thrown at run time at line //2.


------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dear,
actually that is an invalid implementation of hashCode() method.

let 2 objects o1,o2 with the values:

o1: s1="a" s2="bb" s3="c"--->s1+s2+s3 is "abbc"
o2: s1="ab" s2="b" s3="c"--->s1+s2+s3 is "abbc"

now---o1.equals(o2) returns true
but "a".hashCode()!="ab".hashCode(); --violates the rule.

got it?

 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both a and c are true.

a. hashCode() implementation is invalid. (see Mahendar's post)
c. Both objects will be stored. They have same hashCode, bud are not equal according to equals().
[ December 01, 2005: Message edited by: Vlado Zajac ]
 
Praveen Ponna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, both a and c are true!

A better version of hashCode is:

public int hashCode()
{
return (s1+s2+s3).hashCode();
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic