Do we have a concept(?) called 'Hashfunction' in 'Hashtable' in Java?

  • Java
  • Thread starter pairofstrings
  • Start date
  • Tags
    Concept Java
In summary: If you are using the built in hash table classes then you don't need to worry about any of this stuff.
  • #1
pairofstrings
411
7
I know what Hashtable is. I have seen the methods that it contains and used these methods. I know the differences between the implementation classes. But I have not come across anything called 'Hashfunction' during the time when I was studying Collections framework. Is this 'Hashfuntion' has something to do with 'Hashtable'?
I saw some videos and the guys were talking about 'Hashcode'(I know little bit about this), 'Compression function', 'Collision handling' and 'Separate chaining', 'Open addressing' and 'Linear probing', 'Double hashing', and they also talk about the performance of hashing..and 'associative arrays'. I am wondering if these concepts have something to do with 'Hashing technique'. I only know that there is something called Hashing technique which helps in making the searches faster. And there are "buckets" in which objects are stored in Hashing technique.
If all of these concepts belongs to Hashtable in Java, is it important to know all of these concepts to create Hashtable?
Thanks for your time.
 
Last edited:
Technology news on Phys.org
  • #2
The Java Object has a hashcode() method which does some kinda default calculation -- I think it produces the value printed for objects that don't implement toString(). When you stuff Objects into a hash table the hashcode() method is called to figure out what hash bucket to use. There is some probability that two objects will have the same code, so hash stores need to be able to rummage through each bucket in order to find the actual object, but the hash code gets you to the right bucket with (usually) one integer compare. All those other concepts are optimizations. You actually don't need to know nutthin bout hashing to use the Java HashMap classes, they just (usually) work...
 
  • #3
All of these words you are using are important for implementing a hashtable. The point of something like Hashtable in java is it should be a black box. You don't need to know what's going on inside. You just need to know it works.

The only thing you might need to care about is when you implement hashcode(), like schip666 said.

http://www.javapractices.com/topic/TopicAction.do?Id=28

But this is only if you are using custom classes as keys I think.
 

Related to Do we have a concept(?) called 'Hashfunction' in 'Hashtable' in Java?

1. What is a hash function in Java?

A hash function in Java is a mathematical algorithm that takes in an input and produces a unique numerical value, called a hash code. This hash code is used to index and store data in a data structure, such as a Hashtable, for efficient retrieval.

2. How is a hash function used in a Hashtable in Java?

A hash function is used in a Hashtable in Java to map keys to their corresponding values. The hash code generated by the hash function is used as an index in the Hashtable to store and retrieve data. This allows for fast retrieval and storage of data in the Hashtable.

3. Why is a hash function important in a Hashtable?

A hash function is important in a Hashtable because it ensures efficient data retrieval and storage. Without a hash function, each key-value pair would have to be searched for individually, resulting in slower performance. The hash function allows for a specific key to be mapped to a specific index, making data retrieval faster.

4. Can a hash function have collisions in a Hashtable?

Yes, a hash function can have collisions in a Hashtable. A collision occurs when two different keys produce the same hash code, resulting in the same index in the Hashtable. This can be resolved by using a technique called chaining, where multiple key-value pairs with the same index are stored in a linked list at that index.

5. Are there different types of hash functions in Java?

Yes, there are different types of hash functions in Java. Some examples include the hashCode() method in the Object class, which generates a hash code based on the object's memory address, and the hash() method in the String class, which generates a hash code based on the characters in the string. There are also custom hash functions that can be created for specific data types.

Similar threads

  • Programming and Computer Science
Replies
20
Views
31K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
879
Back
Top