File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Hash Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hash Code" Watch "Hash Code" New topic
Author

Hash Code

Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 30

even after a long search on net i am unable to get the fact :

as said Hash Code: it is a area of memory (can be called as bucket in which object is placed). I am confused actually at which time that bucket is decided , because what i think , as soon as the brand new
object is created their bucket number should be specified , but when we write some printStatement in hashCode() it is not running at object creation. when we try to print the object reference then it is called (due to toString()'s calls hashCode()). So the question is -- in which bucket it'll be placed initially ?.

note : suppose we are returning same int value from that hashCode();

Thanks a lot
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Buckets aren't related to objects in general. Buckets are a feature specifically of a few collection classes that use hashing algorithms - most notably HashMap and HashSet. A HashSet contains a number of buckets and a formula for selecting a bucket given a hash code.

So when the HashSet is created, a number of buckets are created. At the point that an object is added to the HashSet, then the hash code is read and the set decides where to put that object. If you never put an object in a hash-based collection, then hashCode() may never be called.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Pramod Kumar Pandey wrote:as said Hash Code: it is a area of memory (can be called as bucket in which object is placed).

A hash code is just a number, it's not an area of memory.

Pramod Kumar Pandey wrote:I am confused actually at which time that bucket is decided , because what i think , as soon as the brand new
object is created their bucket number should be specified , but when we write some printStatement in hashCode() it is not running at object creation.

The "buckets" you are talking about have to do with how hash-based collections such as HashMap store data. A HashMap has a list of buckets, in which it stores objects, according to their hash code. Note that the hash code is not the same as the bucket, it is used to identify in which bucket to store the object. The hash code is (more or less) the bucket number, not the bucket itself. Those buckets are part of the HashMap. The objects themselves don't know anything about buckets.

Pramod Kumar Pandey wrote:note : suppose we are returning same int value from that hashCode();

If you do that, all objects would be placed in the same bucket in the HashMap, which make the HashMap work inefficiently.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 30

sir when we try to print reference , then hashCode() called it means that the object is being accessed from some bucket . so which bucket ? sir if a object is not going to participate in a collection then why their hasCode() code is running when priting their reference ?. Does it has no meaning without collection classes ?.
Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 30

Jesper de Jong wrote:
Pramod Kumar Pandey wrote:as said Hash Code: it is a area of memory (can be called as bucket in which object is placed).

A hash code is just a number, it's not an area of memory.

Pramod Kumar Pandey wrote:I am confused actually at which time that bucket is decided , because what i think , as soon as the brand new
object is created their bucket number should be specified , but when we write some printStatement in hashCode() it is not running at object creation.

The "buckets" you are talking about have to do with how hash-based collections such as HashMap store data. A HashMap has a list of buckets, in which it stores objects, according to their hash code. Note that the hash code is not the same as the bucket, it is used to identify in which bucket to store the object. The hash code is (more or less) the bucket number, not the bucket itself. Those buckets are part of the HashMap. The objects themselves don't know anything about buckets.

Pramod Kumar Pandey wrote:note : suppose we are returning same int value from that hashCode();

If you do that, all objects would be placed in the same bucket in the HashMap, which make the HashMap work inefficiently.


Thanks for your nice effort but .
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Pramod Kumar Pandey wrote:sir when we try to print reference , then hashCode() called it means that the object is being accessed from some bucket . so which bucket ? sir if a object is not going to participate in a collection then why their hasCode() code is running when priting their reference ?. Does it has no meaning without collection classes ?.

Assuming you haven't overridden the toString method in your class, then the object will use the Object implementation of toString which is described as follows:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

so in this case it is the toString method that is calling the hashCode method and is nothing to do with collections or buckets.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Pramod Kumar Pandey wrote:sir when we try to print reference , then hashCode() called it means that the object is being accessed from some bucket .

No, it is not being accessed from "some bucket". The hashCode() method is just a method that returns an integer, there's no magic happening.

When you print an object and its toString() method is not overridden, then the toString() method from class java.lang.Object is called. That method prints something that looks like <classname>@<hashcode> (where the hash code is printed in hexadecimal). It probably looks something like this:

There's nothing special going on, and nothing that has anything to do with buckets.
Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 30

Dear Sir one last thing .. How we determine that a collection class has how many bucket (fixed or varies) ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Well, generally we don't, because generally we don't care about that sort of thing. Most of us just let the collection get on with its work while we get on with ours. However if you're curious then, first of all: Only Map-type collections are likely to use buckets at all. List-type collections won't use buckets. And if you really want to know that, I suggest (a) looking at the collection's source code, and (b) using a debugger to see how it works in a real program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hash Code
 
Similar Threads
Regarding Hashcode and Equals
about map (HashMap to be specific)
Duplicate entries in HashSet
Why does Java’s hashCode() in String use 31 as a multiplier?
General question about HashMaps in java