Win a copy of Head First Android this week in the Android 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

HashMap prob

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
public class HashTest{
int hashCode;
public HashTest(int hashCode){
this.hashCode = hashCode;
}
public boolean equals(Object other){
if(other instanceof HashTest){
return ((HashTest)other).hashCode == this.hashCode;
}
return false;
}
public static void main(String [] args) {
HashTest ob1 = new HashTest(3);
HashTest ob2 = new HashTest(2);
HashTest ob3 = new HashTest(1);
Map mp = new HashMap();
mp.put(ob1,"one");
mp.put(ob2,"two");
mp.put(ob3,"three");
ob1 = new HashTest(1);
System.out.println(mp.get(ob1));
}
}
a) one
b) two
c) three
d) null
e) Compilation fails


Why the answere is null?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources.
 
Debajit Paul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javabeat generic questions.....1.5
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It returns null because the object that is used as key in the "get" method does not exist as key in the HashMap. The equals method plays no part in this (it's never called).

If you comment out the "ob1 = new HashTest(1);" line, then it will return "one".
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The get method evaluates the hashCode before evaluating equals.

If you override hashCode e.g.
public int hashCode() { return (hashCode * 17) ; }

the output would be "three"

good read : http://www.javaranch.com/journal/200407/ScjpTipLine-hashCodesUncovered.html
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can print out the hashcode of ob1 just like this:


The hashCode is different from the previous hashCode after you assign a new HashTest object.

"The hashcode value of an object is used by some collection classes.Although you can think of it as kind of an object ID number,it isn't necessarily unique.Collections such as HashMap and HashSet use the hashcode value of an object to determine how the object should be stored in the collection,and the hashcode is used again to help locate the object in the collection."


because you don't override the hashCode() method,obj1#1 and obj1#2 are in differnt "buckets", they are not equal.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic