Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What hashing function does Java use to implement Hashtable class?

From the book CLRS ("Introduction to Algorithms"), there are several hashing functions, such as mod, multiply, etc.

What hashing function does Java use to map the keys to slots?

I have seen there is a question here Hashing function used in Java Language. But it doesn't answer the question, and I think the marked answer for that question is wrong. It says that hashCode() let you do your own hashing function for Hashtable, but I think it is wrong.

The integer returned by hashCode() is the real key for Hashtble, then Hashtable uses a hashing function to hash the hashCode(). What this answer implies is that Java give you a chance to give Hashtable a hashing function, but no, it is wrong. hashCode() gives the real key, not the hashing function.

So what exactly the hashing function does Java use?

like image 225
Jackson Tale Avatar asked Feb 20 '12 15:02

Jackson Tale


People also ask

What is hashing in Hashtable Java?

Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

How is hash table implemented in Java?

The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. It is similar to HashMap, but is synchronized.

Which hash function does Java use?

In Java, one of the most basic computer science concepts is “hashing”. Java's hashCode() function does the hashing for us. By employing hashing techniques, it is possible to map data to a representational integer value.

What data structure is used to implement a Hashtable in Java?

Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located from.


1 Answers

When a key is added to or requested from a HashMap in OpenJDK, the flow of execution is the following:

  1. The key is transformed into a 32-bit value using the developer-defined hashCode() method.
  2. The 32-bit value is then transformed by a second hash function (of which Andrew's answer contains the source code) into an offset inside the hash table. This second hash function is provided by the implementation of HashMap and cannot be overridden by the developer.
  3. The corresponding entry of the hash table contains a reference to a linked list or null, if the key does not yet exist in the hash table. If there are collisions (several keys with the same offset), the keys together with their values are simply collected in a singly linked list.

If the hash table size was chosen appropriately high, the number of collisions will be limited. Thus, a single lookup takes only constant time on average. This is called expected constant time. However, if an attacker has control over the keys inserted into a hash table and knowledge of the hash algorithm in use, he can provoke a lot of hash collisions and therefore force linear lookup time. This is why some hash table implementations have been changed recently to include a random element that makes it harder for an attacker to predict which keys will cause collisions.

Some ASCII art

key.hashCode()      |      | 32-bit value      |                              hash table      V                            +------------+    +----------------------+ HashMap.hash() --+                | reference  | -> | key1 | value1 | null |                  |                |------------|    +----------------------+                  | modulo size    | null       |                  | = offset       |------------|    +---------------------+                  +--------------> | reference  | -> | key2 | value2 | ref |                                   |------------|    +---------------------+                                   |    ....    |                       |                                                       +----------------+                                                       V                                                     +----------------------+                                                     | key3 | value3 | null |                                                     +----------------------+ 
like image 167
Niklas B. Avatar answered Sep 19 '22 10:09

Niklas B.