File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on hascode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question on hascode()" Watch "question on hascode()" New topic
Author

question on hascode()

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
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

Joined: Oct 21, 2003
Posts: 8
And the question is?
velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
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

Joined: Sep 27, 2005
Posts: 73
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?



SCJP- 98%<br />SCWCD-92%
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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

Joined: Nov 26, 2005
Posts: 11
Yes, both a and c are true!

A better version of hashCode is:

public int hashCode()
{
return (s1+s2+s3).hashCode();
}


scjp1.4
 
 
subject: question on hascode()