Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 23 August 2017

Difference Between HashMap and Hashtable

What is Difference Between HashMap and Hashtable in Java?

HashMap vs Hashtable

Here we will discuss, What is difference between HashMap and Hashtable in java. This is the most frequently asked question in any java interviews.


So let's start, What is the difference between hashmap and hashtable class?

Java HashMap

  • HashMap is non-synchronized i.e multiple threads can access it simultaneously. It is not a thread-safe.
  • HashMap allows one null key and multiple null values.
  • HashMap extends AbstractMap class.
  • HashMap provides fast performance.
  • HashMap is a new class and introduced in jdk 1.2.
  • HashMap returns the only iterator to traverse.
  • The iterator of HashMap is fail-fast and it throws ConcurrentModificationException.
  • It is good for single threaded applications but if you want to use HashMap class in multithreaded applications, You will have to use Collections.synchronizedMap(hashMap).


Now let's understand HashMap vs Hashtable with examples.

Java HashMap Example

This is the simple example of hash map class where we will insert some key and values and then traverse all the elements from the map.

import java.util.*;
class Demo
{
public static void main(String args[])
{
//declaring HashMap
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1000, "suman");//inserting key and value
hm.put(3000, "hari");
hm.put(99, "pooja");
hm.put(5000, "kiran");
hm.put(20000, "bheem");
for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output: 20000 bheem
              99 pooja
              1000 suman
              3000 hari
              5000 kiran


Note: HashMap class contains only unique elements i.e not duplicate elements and doesn't maintain insertion order of an element.


Java Hashtable

  • Hashtable is a synchronized i.e multiple threads can access it one by one. It is thread-safe.
  • Hashtable doesn't allow any null key and null values.
  • Hashtable extends Dictionary class.
  • Hashtable provides slow performance.
  • Hashtable is a legacy class.
  • Hashtable returns both Iterator as well as Enumeration for traversal.
  • Enumeration for Hashtable is not fail-fast.
  • It is good for the multithreaded environment.



Hashtable Example in Java

This is java hashtable example Where we will insert some elements and then traverse all the elements from the table.

import java.util.*;
class Test
{
public static void main(String args[])
{
//declaring Hashtable
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
ht.put(20, "red");//inserting elements
ht.put(10, "black");
ht.put(50, "yellow");
ht.put(7, "pink");
ht.put(90, "brown");
for(Map.Entry m : ht.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output: 10 black
             20 red
             7 pink
             50 yellow
             90 brown

Note: Hashtable class contains only unique elements i.e not duplicate elements and it doesn't maintain any insertion order of an element.


So there are many differences between HashMap and Hashtable in java and you can see other differences topics in collection framework like Difference between ArrayList and LinkedList, ArrayList and Vector, etc.

Similarities Between HashMap and Hashtable class

  1. Both implements Map interface.
  2. Both don't maintain insertion order of an element.
  3. Both store the data in the form of key and value pairs.
  4. Both using the hashing technique to store the key & value pairs.
  5. Both contain only unique elements i.e cannot allow duplicate elements
Share:

2 comments:

  1. Really very helpful article.

    ReplyDelete
  2. Great Article android based projects

    Java Training in Chennai

    Project Center in Chennai

    Java Training in Chennai

    projects for cse

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate