Skip to main content

What is a java HashMap and How it works ?

What is a java HashMap and How it works ?


What is a HashMap ?

            The HashMap in Java is one of the most popular Collection class among Java programmers. The HashMap is a data structure, based on hashing, which allows you to store an object as key value pair, an advantage of using HashMap is that you can retrieve object on constant time i.e. O(1) if you know the key. The HashMap class implements Map interface and supports Generics from Java 1.5 release, which makes it type safe. There are a couple of more Collections, which provides similar functionalities like HashMap, which can also be used to store key value pair. Hashtable is one of them, but Hashtable is synchronized and performs poorly in a single threaded environment. one relatively new is ConcurrentHashMap, which provides better performance than Hashtable in a concurrent environment and should be preferred.

How HashMap works ?

            In Hashmap objects are stored by calling put(key, value) method of HashMap and retrieved by calling get(key) method. When we call put method, hashcode() method of the key object is called so that hash function of the map can find a bucket location to store value object, which is actually an index of the internal array, known as the table. HashMap internally stores mapping in the form of Map.Entry object which contains both key and value object. When you want to retrieve the object, you call the get() method and again pass the key object. This time again key object generate same hash code (it's mandatory for it to do so to retrieve the object and that's why HashMap keys are immutable e.g. String) and we end up at same bucket location. If there is only one object then it is returned and that's your value object which you have stored earlier. Things get little tricky when collisions occur. It's easy to answer this question if you have read good books on data structure and algorithms like this one. If you know how hash table data structure works then this is a piece of cake.

·         INTERNAL STORAGE

o   The JAVA HashMap class implements the interface Map<K,V>. The main methods of this interface are:
§  V put(K key, V value)
§  V get(Object key)
§  V remove(Object key)
§  Boolean containsKey(Object key)

HashMaps use an inner class to store data: the Entry<K, V>. This entry is a simple key-value pair with two extra data

o   A reference to another Entry so that a HashMap can store entries like singly linked lists
o   A hash value that represents the hash value of the key. This hash value is stored to avoid the computation of the hash every time the HashMap needs it.



In plain English all the keys with the same hash value are put in the same linked list (bucket).

Keys with different hash values can end-up in the same bucket.

When a user calls put(K key, V value) or get(Object key), the function computes
the index of the bucket in which the Entry should be. Then, the function iterates
through the list to look for the Entry that has the same key (using the equals()
function of the key).

In the case of the get(), the function returns the value associated with the entry (if
the entry exists).

In the case of the put(K key, V value), if the entry exists the function replaces it
with the new value otherwise it creates a new entry (from the key and value in
arguments) at the head of the singly linked list.

This index of the bucket (linked list) is generated in 3 steps by the map:

·         It first gets the hashcode of the key.
·         It rehashes the hashcode to prevent against a bad hashing function from
the key that would put all data in the same index (bucket) of the inner
array
·         It takes the rehashed hash hashcode and bit-masks it with the length
(minus 1) of the array. This operation assures that the index can’t be
greater than the size of the array. You can see it as a very computationally
optimized modulo function.


hash (getting from hash(key.hashCode())) is used to find the bucket location at which the Entry object is stored . Entry object stores in the bucket like this (hash,key,value,bucketindex) .


In Summary
1. HashMap works on the principal of hashing
2. HashMap uses the hashCode() method to calculate a hash value. Hash value is calculated using the key object. This hash value is used to find the correct bucket where Entry object will be stored
3. HashMap uses the equals() method to find the correct key
4. more than one key have the same hash value, Entry objects are stored as a linked-list with in a same bucket. With in a bucket values are stored as Entry objects which contain both key and value.

Comments

Popular posts from this blog

An Introduction to Spring Framework

An Introduction to Spring Framework   What is Spring ? Spring is an application framework . Unlike single-tier frameworks such as Struts or Hibernate, Spring aims to help structure whole applications in a consistent, productive manner, pulling together best-of-breed single-tier frameworks to create a coherent architecture. Why Spring ? The Spring Framework is an open source application framework that aims to make J2EE development easier. We’ll look at the motivation for Spring, its goals, and how Spring can help you develop high-quality applications quickly. Using J2EE “out of the box” is not an attractive option. Many J2EE APIs and services are cumbersome to use. J2EE does a great job of standardizing low-level infrastructure, solving such problems as how can Java code access transaction management without dealing with the details of transactions. But J2EE does not provide an easily usable view for application code.That is the role of an application framework, such a

Apache Maven

Introduction to Apache Maven   What is maven? Maven is a project management tool which encompasses a project object model, a set of standards, a project life cycle, a dependency management system, and logic for executing plugin goals at defined phases in a life cycle. When you use Maven, you describe your project using a well-defined project object model, Maven can then apply cross-cutting logic from a set of shared (or custom) plugins. The great majority of Maven users are going to call Maven a “build tool”: a tool used to build deployable artifacts from source code. Build engineers and project managers might refer to Maven as something more comprehensive: a project management tool. What is the difference? A build tool such as Ant is focused solely on preprocessing, compilation, packaging, testing, and distribution. A project management tool such as Maven provides a super set of features found in a build tool. In addition to providing build capabilities, Maven can also ru

Liscov Substitution principle

Liscov Substitution Principle  Inheritance is one of the important OOP concept. Liscov Substitution Principle is applied when using inheritance in our program. We must make sure that the child classes just extend without replacing the functionality of parent class. Otherwise the child classes can produce undesired effects when they are used in existing program modules. child component must be completely substitutable for their parent components . As an example if we take the classes " Rectangle" and "Square" In mathematics square is also a kind of rectangle. which makes the square class a child of rectangle class. rectangle class has some methods and they might be over ridden by the square class. if we have a function draw() and we do not know what shape will be returned but it will surely be a rectangle. If we give the length = 10 cm and width = 5 cm violating the Liscov Substitution principle is when the first length entered will be taken as the length of