Online Tutorials & Training Materials | STechies.com
Register Login

HashMap vs Hashtable vs TreeMap vs LinkedHashMap

|| || 0

HashMap vs Hashtable vs TreeMap vs LinkedHashMap
Stechies

This tutorial explains the what are the various data structure and differences between those data structures in Java such as HashMap vs TreeMap vs Hashtable LinkedHashMap with programming examples. 

Difference between HashMap vs TreeMap vs Hashtable LinkedHashMap in Java

 

Hash map

Hash table

Linked hashmap

Tree map

Basic Working

On  insertion order

not working on  insertion order

It maintains insertion order

Sorted by order on key

Performance

organisation not synchronized 

It is a synchronized.

As it maintains insertion order time, space are taken care as  works with internally Doubly Linked

Key-based sorting provided by java

 

 

Operation

It works faster

Slower in compare to other maps but recommended in multithread

Time & space overhead maintain by doubly linked lists

Operations cost performance. To sort comparator used

Null values

Null allowed 

one in key but multiple in values  

Null not allowed try to store return  runtimeException NullPointerException

Null allowd 

one in key but multiple in values   .

Not allowed in key but many allowed in values to attempt to store null key runtimeException NullPointerException

Uses

Buckets are used

 

Buckets are used

    

Doubly linked lists are used

Rectangles are used with red and black colour

Interface

Implemented java.util.map

 implemented java.util.map

Implemented
java.util.map

TreeMap implemented
java.util.Map
java.util.SortedMap
java.util.NavigableMap

What is Hash Map in Java

Hash map is a type of data structure which helps us for faster retrieval of information which is mapped in the form of Keys to values; to access a value from this data structure we should know the key prior.

Hashing is basically a technique where the large string or information converted into small strings; this shorter value helps to faster execution of indexing and searches. There is no guarantee to order of the map, as it is unsynchronised and also permits null values. It does not return the keys and values in which manner as they are stored their corresponding values, so it not an ordered collection too.

Example of Hash Map

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;

public class Details {
   public static void main(String args[]) {
            HashMap<Integer, String> hsap = new HashMap<Integer, String>();
      hsap.put(1, "Chave");
      hsap.put(2, "Rohtas");
      hsap.put(8, "sohrab ");
      hsap.put(9, "vijay");
      hsap.put(3, "Ahuja");
   Set set = hsap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }
      String var= hsap.get(2);
      System.out.println("indexd value 2 is: "+ var);
           hsap.remove(3);
      System.out.println("Map key and values after indexed removal:");
      Set set2 = hsap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
          System.out.println(mentry2.getValue());
       }
   }
}

Output is

key is: 1 & Value is: Chave
key is: 2 & Value is: Rohtas
key is: 3 & Value is: Ahuja
key is: 8 & Value is: sohrab
key is: 9 & Value is: vijay
indexd value 2 is: Rohtas

Map key and values after indexed removal:

Key is: 1 & Value is: Chave
Key is: 2 & Value is: Rohtas
Key is: 8 & Value is: sohrab
Key is: 9 & Value is: vijay

What is Tree Map in Java?

In java Tree map idea is used to display the large amount of data according to rank or you can say according to hierarchically structured. In computing Tree map this information visualization tree mapping method is used, where hierarchal nested data is displayed in the tree structure with help of nested rectangle figures are used.

Size of the rectangle is used according to level, like to display a tree it will be a bigger rectangle and for the branch size is shorter and so on. You can say data as a set of nested rectangles and for a leaf data representation proportion size and the colour is used. The red and black colour is used.

As a particular, relevant to show with certain colour with the help of this space can be used more efficiently we can say that on one screen we can show thousands of items. Different datasets use different colour in the rectangle which distinguishes and simple datasets else it is very tough to identify.

Tree map is basically derived from Tiling Algorithm where an aspect ratio is used to show the rectangle and colour to make the sensible output of input data, so the aspect ratio is highly maintained.

Example of Tree Map

import java.util.*; 
public class TMap{ 
 public static void main(String args[]){ 
   TreeMap<Integer,String> map1=new TreeMap<Integer,String>();   
      map1.put(10,"Amitabha");   
      map1.put(50,"Jaya");   
      map1.put(30,"Abhishek");   
      map1.put(40,"Ashwarya");   
        for(Map.Entry m:map1.entrySet()){   
       System.out.println(m.getKey()+" "+m.getValue());   
      }   
 } 
} 

Output

 $javac TMap.java
$java -Xmx128M -Xms16M TMap

10 Amitabha
30 Abhishek
40 Ashwarya
50 Jaya

What is  Hash Table in Java?

Hash table is again from the class of Data structures where data is stored on the basis of Key and value mapping phenomena, Hash table is based on Hashing technique where similar data is stored in form of array. It is a synchronised data structure. Unlike the Hash Map it does not allow the null values.

As we know the data structure is used to store the data, data management and faster retrieval, it stores the data in an array format and associative manner, but it is slow in comparison with other provided data structures. Using a unique index value which helps in faster execution.

It performs different operations insert and search to data operations. It converts large data range to range of array or similar type of data index key values.

Example Program for Hash Table

import java.util.*; 
public class Hstable{ 
 public static void main(String args[]){ 
  Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); 
  ht.put(110,"Ronak"); 
  ht.put(112,"javed"); 
  ht.put(111,"babua"); 
  ht.put(113,"jassi"); 
  for(Map.Entry m:ht.entrySet()){ 
   System.out.println(m.getKey()+" "+m.getValue()); 
  } 
 } 
} 

Output

$javac Hstable.java
$java -Xmx128M -Xms16M Hstable
113 jassi
112 javed
111 babua
110 Ronak

LinkedHashMap in Java

It is quite similar to HashMap but with some of the additional features to maintain the order to insert elements into. It has quick insert, search and deletion advantage along but it does not track order of insert whereas the LinkedHashmap access them in the way as they insertion done.

  • It contains unique elements.
  • It can have many null values but only one null in key. 
  • It is cannot be synchronised.

To enhance the working of LinkedHashmap it includes all facilities of both Hashtable and linked hash map. LinkedhashMap has additional functionality is pointer is added into it for the next and previous entries.Example Program of Linkedhashmap

Example Program of Linkedhashmap

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;

public class LHMDemo {

    public static void main(String args[]) {
      LinkedHashMap<Integer, String> hmd =
                 new LinkedHashMap<Integer, String>();
         hmd.put(10, "jai");
         hmd.put(30, "Vikas");
         hmd.put(12, "Shelly");
         hmd.put(1, "Shivek");
         hmd.put(20, "Jhon");
         Set set = hmd.entrySet();
         Iterator iterator = set.iterator();
         while(iterator.hasNext()) {
            Map.Entry me = (Map.Entry)iterator.next();
            System.out.print("Key is: "+ me.getKey() +
                    "& Value is: "+me.getValue()+"n");
         }
    }
}

Output

$javac LHMDemo.java
$java -Xmx128M -Xms16M LHMDemo
Key is: 10& Value is: jai
Key is: 30& Value is: Vikas
Key is: 12& Value is: Shelly
Key is: 1& Value is: Shivek
Key is: 20& Value is: Jhon

Similarities in HashMap vs Hashtable vs TreeMap vs LinkedHashMap in Java

  • All maps are type of data structure.
  • Data is store in all map but different technique is adopted.
  • All map works on key and values basis.
  • Indexing and sorting done on key basis.
  • All allow faster retrieval of information.
  • Large and similar type data is stored with key & values.

 


Related Articles