This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

question on hascode()

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
------------------------------------------------------------------
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the question is?
 
velan vel
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
------------------------------------------------------------------
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic