Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

question on hascode()

 
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
------------------------------------------------------------------
 
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
------------------------------------------------------------------
 
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?

 
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 ]
 
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();
}
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic