Hashmap Concept Simplified in Java



Hashmap is one of the data structure provided by java. It contains a set of unique keys and corresponding values. Once successfully stored in a hashmap,they are stored as key-value pair and they can be retrieved at any point of time with the key.If no element is present in the Hashmap, it will throw a NoSuchElementException.
Hashmap stores objects as object reference hence we can not store any primitive datatypes instead we need to store the Wrapper classes for the primitive data types.
Hashmap uses Hashing technique internally hence the searching becomes faster.HashMap extends abstract class AbstractMap which implementation the Map interface.it also implements Cloneable and Serializable interface. HashMap doesn’t allow duplicate keys but allows duplicate values.HashMap allows one null key to be inserted.The order of the Hashmap is not as we insert the keys.

General form of Hashmap is :
HashMap<Object, Object> myMap = new HashMap<>();
HashMap:
Key1------>value1
Key2------>value2
Key3------>value3
.
.
.
KeyN------>valueN

When we say HashMap can contain any numbers of objects inside it, so there comes two important things to remember:
1. The initial capacity
2. How HashMap grows: Rehashing
3. When the HashMap grow: The load factor

The initial capacity of the HashMap is not fixed for every case. But it is assigned a higher number so that HashMap does not have to increase its size.Commonly given initial size is 16.

In case JVM sees that the objects are getting added in a huge manner, it will try to increase the capacity of the HashMap by using rehashing technique. In this process , the capacity becomes double the initial capacity.

Load factor determines when to call rehashing technique to increase it's size.This is required as the rehashing takes some time. The load factor falls in the range of 0 to 1. The majorly used load factor is .75 that gives JVM and the coder with an optimum performance for the HashMap.
HashMap class provides 4 constructs as follows:
No Constructor Name Constructor Description Initial Capacity Load Factor
1 HashMap() default constructor 16 .75
2 HashMap(int initial capacity) creates a HashMap with the initial capacity provided in the argument capacity .75
3 HashMap(int initial capacity, float loadFactor) creates a HashMap with the initial capacity and loadFactor provided capacity loadFactor
4 HashMap(Map myMap) creates a HashMap from the specification from another Map-myMap as per Map-myMap as per Map-myMap
The HashMap class provides the following methods to work with:
No Method Name Method Description Return Comment
1 get(Object Key) Returns the corresponding value for the key Object
2 put(Object Key,Object Value) Insert the value with association with the key in the HashMap Object
3 keySet() Returns the set view of the HashMap Set
4 values() Returns the values of the HashMap
5 containsKey(Object key) Checks if the key is present in the HashMap boolean true or false
6 remove(Object key) removes the key from the HashMap Object Removes the key and value from HashMap
7 isEmpty() Checks if HashMap is empty boolean true or false
8 containsValue(Object value) Checks if HashMap has a mapping key for the value speficied boolean true or false
9 clone() clones a HashMap Object
10 entrySet() Returns the set view of the HashMap Set
11 size() Returns size of the HashMap int
12 putAll(Map myMap) copy all elements from myMap to current HashMap
13 clear(() clears/remove all mappings from the HashMap void


import java.util.HashMap;
public class HashMapTest{
 public static void main(String []args){
        //let us create a HashMap of Roll Number-Name 
        HashMap rollNumber=new HashMap<>();
        //check the initial values- it should be blank
        System.out.println("Initial HashMap contains"+rollNumber);
        //Add member to the class
        rollNumber.put(1,"Avishek");
        rollNumber.put(2,"Bishal");
        rollNumber.put(3,"Parvez");
        rollNumber.put(4,"Mainul");
        //check the updated Hashmap
        System.out.println("updated HashMap contains"+rollNumber);
        // test if the HashMap is having a valid key
        boolean flag1=rollNumber.containsKey(1);
        System.out.println("The key 1 present in the HashMap "+flag1);
        //negative test
        boolean flag2=rollNumber.containsKey(6);
        System.out.println("The key 6 present in the HashMap "+flag2);
        //get values from HashMap
        String name=rollNumber.get(1);
        System.out.println("The value correspond to key 1 "+name);
        //remove a key from HashMap
        rollNumber.remove(2);
       //check the contents of the HashMap
        System.out.println("updated HashMap contains"+rollNumber);
        //remove everything from the HashMap
        rollNumber.clear();
         System.out.println("updated HashMap contains"+rollNumber);
     }
}
The output of the code is :
$javac HashMapTest.java
$java -Xmx128M -Xms16M HashMapTest
Initial HashMap contains{}
updated HashMap contains{1=Avishek, 2=Bishal, 3=Parvez, 4=Mainul}
The key 1 present in the HashMap true
The key 6 present in the HashMap false
The value correspond to key 1 Avishek
updated HashMap contains{1=Avishek, 3=Parvez, 4=Mainul}
updated HashMap contains{}
Hashmap Concept Simplified in Java Hashmap Concept Simplified in Java Reviewed by Animesh Chatterjee on August 29, 2018 Rating: 5

No comments:

Powered by Blogger.