Home >Java >javaTutorial >Hashing Function in Java
Hashing function in Java was created as a solution to define & return the value of an object in the form of an integer, and this return value obtained as an output from the hashing function is called as a Hash value. Every Hashing function returns an integer of 4 bytes as a return value for the object. Any two objects of the same type will have the same integer value as Hashing function’s output, and similarly, different objects will have different hash values. One cannot derive the objects from the hash value, and this makes the hashing function an irreversible function.
What is a Hashing Function?
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
A Hash Function can be defined as a function that returns an integer value corresponding to an Object. Hash Function always returns the same integer value for the same object. The integer value returned by the hash function is called Hash Value. Following are the important points regarding the Hash function:
Here are the common applications of hash functions:
Almost every programming language contains hash-based data structures. For example, java contains a Hash table, Hash Map, Hash Set, Tree Set data structures which are based on the Hash function. These data structures are the Key-Value design, where each key is unique, whereas the same value can exist for multiple keys.
This algorithm is used in a data integrity check. This algorithm takes a message of any length as input and produces a fixed-length (128-bit) data as output. Examples of the message-digest algorithms include MD2, MD4, MD5, and MD6.
This algorithm is used for data security and is used in applications and protocols like Secure Socket Layer (SSL). SHA-0, SHA-1, SHA-2, and SHA-3 are common categories of the Secure Hash Algorithm.
Let’s consider a login scenario in which when a password is entered to authenticate a user, a hash value of the entered password is computed and is sent over the network to the server where the hash of the original is stored. This is done to ensure that no sniffing is done when a password is sent from the client to the server.
Since different keywords are used in a programming language, in order to differentiate between these keywords and identifiers, the compiler uses a hash set that is implemented using a hash table to store all these keywords and identifiers.
It is a searching algorithm that makes use of hashing to search for one or more patterns in a given string. It is one of the most commonly used Algorithms.
These interfaces contain functions that are used to compare two objects at a time. The return value of these functions may be negative, zero or positive based on whether a given object is less than, equal to, or greater than the object we are comparing to. Internally comparator and comparable interfaces use a hash function to compare objects from one another.
The priority queue is unlike a normal queue that follows FIFO (First in First out) order. In priority queue elements are arranged in custom order based on their priority, which is internally implemented using comparable and comparator which interns are based on Hash Functions.
Here are some general design principles for creating hash functions:
public int hashCode (){ //Logic goes here }
A hash collision occurs when two or more objects return the same hash value. Let us take an example of a java hash map that stores data in key-value pairs. When we put an object in a hash map, the key’s hash value is computed and based on this hash value bucket location to store the value object is found. Objects having different hash values must go into different buckets. When two or more objects have the same hash value, they are stored in the same bucket location using an additional data structure called a linked list. All objects having the same hash value are chained together using a linked list. This mechanism is called chaining. Following are the ways to handle collisions is a hash function:
The following are the advantages of hashing:
Apart from advantages, there are also some limitations of hashing:
The above is the detailed content of Hashing Function in Java. For more information, please follow other related articles on the PHP Chinese website!