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 Online material on Hashcode and Equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Online material on Hashcode and Equals method" Watch "Online material on Hashcode and Equals method" New topic
Author

Online material on Hashcode and Equals method

vijaya dev
Ranch Hand

Joined: Jun 22, 2004
Posts: 33
Hi everyone,
I was wondering if anyone can suggest me good online material on Hashcode and Equals method. I studied the material in K&B..its good..As hashcode topic is new to me it will be great if I get more information on this topic.
Thanks,
vijaya


SCJP 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I can't offer an on-line tutorial, but I can offer a short mock exam that covers hashcodes. Please see Chapter 18 on this page.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Check Effective Java Programming Language Guide by Joshua Bloch
Here is the sample chapter covering methods like equals, hashCode, toString, clone etc


Mani
Quaerendo Invenietis
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
http://www.jchq.net/certkey/0902certkey.htm


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
How is this?


SCJP Tipline, etc.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Excellent article Cory.
vijaya dev
Ranch Hand

Joined: Jun 22, 2004
Posts: 33
Thank you very much for your replies. Still I am not clear with the concept of Hashcode. In the example provided by corey
I have the following questions(dumb questions !!).
1. When is the Equal method called and to whom does it return the value
2. When is the Hash method called and to whom does it return the value
3. What steps does the h.get(key[i]) follow to get the value for the given key.

I appreciate any help provided to help me understand this concept.

Thanks,
vijaya


import java.util.Hashtable;
import java.util.Date;

public class MyObject
{
int a;

public MyObject(int val)
{
a = val;
}

public boolean equals(Object o)
{
boolean isEqual = false;

if ( o instanceof MyObject )
{
if ( ((MyObject)o).a == a )
{
isEqual = true;
}
}

return isEqual;
}

public int hashCode()
{
return a;
}

public static void main(String[] args)
{
Hashtable h = new Hashtable();

MyObject[] keys =
{
new MyObject(11),
new MyObject(12),
new MyObject(13),
new MyObject(14),
new MyObject(15),
new MyObject(16),
new MyObject(17),
new MyObject(18),
new MyObject(19),
new MyObject(110)
};

for ( int i = 0; i < 10; i++ )
{
h.put(keys[i], Integer.toString(i+1));
}

long startTime = new Date().getTime();

for ( int i = 0; i < 10; i++ )
{
System.out.println(h.get(keys[i]));
}

long endTime = new Date().getTime();

System.out.println("Elapsed Time: " + (endTime - startTime) + " ms");
}
}
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by vijaya dev:
1. When is the Equal method called and to whom does it return the value
2. When is the Hash method called and to whom does it return the value
3. What steps does the h.get(key[i]) follow to get the value for the given key.


In each case, you're asking about the inner workings of the hash implementation - in this case, Hashtable, but HashMap is another implementation of the same thing.

The hash implementation is the one that invokes the hashcode() method and the equals() method - it's all done behind the scenes and you need not worry about it. All you need to do is provide valid equals() and hashcode() methods.

What steps the get() method follows are also up to the implementation of the hash table you're using. In general, the get() method will invoke hashcode() on the object you pass to it and it will use the resulting value to index the internal hash table and return to you the object you desire.
 
GeeCON Prague 2014
 
subject: Online material on Hashcode and Equals method