Monday, March 17, 2014

Hash Map and Hash Tables





Difference between Hashtable and HashMap in java

One of the common interview question is "What are differences between Hashtable and HashMap".When I started using them,I used any of them irrespective of their differences.Afterwards i found noticeable differences between them which can affect your performance of application. .Before we actually see differences,let me give you brief introduction of both.

HashMap

HashMap implements Map interface which maps key to value.It is not synchronized and is not thread safe.Duplicate keys are not allowed and null keys as well as value are allowed.
  1. HashMap<Interger,String> employeeHashmap=new HashMap<Integer,String>();  
  2. employeeHashmap.put(1,"Arpit");  
  3. employeeHashmap.put(2,null);  // will work fine  

Hashtable

Hashtable implements Map interface which maps key to value.It is synchronized and thread safe.Duplicate keys are not allowed and null key is not allowed.
  1. Hashtable<Interger,String> employeeHashmap=new Hashtable<Integer,String>();  
  2. employeeHashmap.put(1,"Arpit");  
  3. employeeHashmap.put(2,null);  //not allowed and will throw NullPointer exception at run time  

Hashtable vs HashMap:

Parameter
Hashtable
HashMap
ThreadSafe
Yes
No
Synchronized
Yes
No
Performance
Due to theadSafe and Synchronized,it is often slower than HashMap
In single threaded environment, it is much faster than Hashtable.So if you do not work in multi thread environment ,then hashMap is recommended
Null key
Do not allow
Allows null key as well as values
Fail fast
enumeration in hashtable is not fail fast
Iterator in hashMap is fail fast
Extends
It extends Dictionary class which is quite old
It extends AbstractMap class
Alternative
No alternative
You can use ConcurrentHashMap for multi thread environment

Some important points need to be discussed.
  • Synchonized meaning only one thread can modify one table  at one point of time.When any thread perform update operation on hashtable then it acquires lock on it and other threads have to wait for lock to be released.
  • Fail-fast iterator means if one thread is iterating over hashmap and other thread trying to modify hashmap structurally it will throw ConcurrentModification Exception and fail immediately.Structurally modification means inserting or deleting elements that can change structure of map.

Can we synchronize HashMap?

Yes,We can synchonized a HashMap also with the help of Collections.synchonizedMap(hashmap) so HashMap can be synchronized by
  1. Map map=Collections.synchonizedMap(hashmap)  


HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

Map is one of the most important data structures. In this tutorial, I will show you how to use different maps such as HashMap, TreeMap, HashTable and LinkedHashMap.
1. Map Overview

There are 4 commonly used implementations of Map in Java SE – HashMap, TreeMap, Hashtable and LinkedHashMap. If we use one sentence to describe each implementation, it would be the following:
  • HashMap is implemented as a hash table, and there is no ordering on keys or values.
  • TreeMap is implemented based on red-black tree structure, and it is ordered by the key.
  • LinkedHashMap preserves the insertion order
  • Hashtable is synchronized, in contrast to HashMap.
  • This gives us the reason that HashMap should be used if it is thread-safe, since Hashtable has overhead for synchronization.
2. HashMap
If key of the HashMap is self-defined objects, then equals() and hashCode() contract need to be followed.
class Dog {
 String color;
 
 Dog(String c) {
  color = c;
 }
 public String toString(){ 
  return color + " dog";
 }
}
 
public class TestHashMap {
 public static void main(String[] args) {
  HashMap<Dog, Integer> hashMap = new HashMap<Dog, Integer>();
  Dog d1 = new Dog("red");
  Dog d2 = new Dog("black");
  Dog d3 = new Dog("white");
  Dog d4 = new Dog("white");
 
  hashMap.put(d1, 10);
  hashMap.put(d2, 15);
  hashMap.put(d3, 5);
  hashMap.put(d4, 20);
 
  //print size
  System.out.println(hashMap.size());
 
  //loop HashMap
  for (Entry<Dog, Integer> entry : hashMap.entrySet()) {
   System.out.println(entry.getKey().toString() + " - " + entry.getValue());
  }
 }
}
Output:
4
white dog - 5
black dog - 15
red dog - 10
white dog - 20
Note here, we add “white dogs” twice by mistake, but the HashMap takes it. This does not make sense, because now we are confused how many white dogs are really there.
The Dog class should be defined as follows:
class Dog {
 String color;
 
 Dog(String c) {
  color = c;
 }
 
 public boolean equals(Object o) {
  return ((Dog) o).color == this.color;
 }
 
 public int hashCode() {
  return color.length();
 }
 
 public String toString(){ 
  return color + " dog";
 }
}
Now the output is:
3
red dog - 10
white dog - 20
black dog - 15
The reason is that HashMap doesn’t allow two identical elements. By default, the hashCode() and equals() methods implemented in Object class are used. The default hashCode() method gives distinct integers for distinct objects, and the equals() method only returns true when two references refer to the same object. Check out the hashCode() and equals() contract if this is not obvious to you.
Check out the most frequently used methods for HashMap, such as iteration, print, etc.
3. TreeMap
A TreeMap is sorted by keys. Let’s first take a look at the following example to understand the “sorted by keys” idea.
class Dog {
 String color;
 
 Dog(String c) {
  color = c;
 }
 public boolean equals(Object o) {
  return ((Dog) o).color == this.color;
 }
 
 public int hashCode() {
  return color.length();
 }
 public String toString(){ 
  return color + " dog";
 }
}
 
public class TestTreeMap {
 public static void main(String[] args) {
  Dog d1 = new Dog("red");
  Dog d2 = new Dog("black");
  Dog d3 = new Dog("white");
  Dog d4 = new Dog("white");
 
  TreeMap<Dog, Integer> treeMap = new TreeMap<Dog, Integer>();
  treeMap.put(d1, 10);
  treeMap.put(d2, 15);
  treeMap.put(d3, 5);
  treeMap.put(d4, 20);
 
  for (Entry<Dog, Integer> entry : treeMap.entrySet()) {
   System.out.println(entry.getKey() + " - " + entry.getValue());
  }
 }
}
Output:
Exception in thread "main" java.lang.ClassCastException: collection.Dog cannot be cast to java.lang.Comparable
 at java.util.TreeMap.put(Unknown Source)
 at collection.TestHashMap.main(TestHashMap.java:35)
Since TreeMaps are sorted by keys, the object for key has to be able to compare with each other, that’s why it has to implement Comparable interface. For example, you use String as key, because String implements Comparable interface.
Let’s change the Dog, and make it comparable.
class Dog implements Comparable<Dog>{
 String color;
 int size;
 
 Dog(String c, int s) {
  color = c;
  size = s;
 }
 
 public String toString(){ 
  return color + " dog";
 }
 
 @Override
 public int compareTo(Dog o) {
  return  o.size - this.size;
 }
}
 
public class TestTreeMap {
 public static void main(String[] args) {
  Dog d1 = new Dog("red", 30);
  Dog d2 = new Dog("black", 20);
  Dog d3 = new Dog("white", 10);
  Dog d4 = new Dog("white", 10);
 
  TreeMap<Dog, Integer> treeMap = new TreeMap<Dog, Integer>();
  treeMap.put(d1, 10);
  treeMap.put(d2, 15);
  treeMap.put(d3, 5);
  treeMap.put(d4, 20);
 
  for (Entry<Dog, Integer> entry : treeMap.entrySet()) {
   System.out.println(entry.getKey() + " - " + entry.getValue());
  }
 }
}
Output:
red dog - 10
black dog - 15
white dog - 20
It is sorted by key, i.e., dog size in this case.
If “Dog d4 = new Dog(“white”, 10);” is replaced with “Dog d4 = new Dog(“white”, 40);”, the output would be:
white dog - 20
red dog - 10
black dog - 15
white dog - 5
The reason is that TreeMap now uses compareTo() method to compare keys. Different sizes make different dogs!
4. Hashtable
From Java Doc:
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
5. LinkedHashMap
LinkedHashMap is a subclass of HashMap. That means it inherits the features of HashMap. In addition, the linked list preserves the insertion-order.
Let’s replace the HashMap with LinkedHashMap using the same code used for HashMap.
class Dog {
 String color;
 
 Dog(String c) {
  color = c;
 }
 
 public boolean equals(Object o) {
  return ((Dog) o).color == this.color;
 }
 
 public int hashCode() {
  return color.length();
 }
 
 public String toString(){ 
  return color + " dog";
 }
}
 
public class TestHashMap {
 public static void main(String[] args) {
 
  Dog d1 = new Dog("red");
  Dog d2 = new Dog("black");
  Dog d3 = new Dog("white");
  Dog d4 = new Dog("white");
 
  LinkedHashMap<Dog, Integer> linkedHashMap = new LinkedHashMap<Dog, Integer>();
  linkedHashMap.put(d1, 10);
  linkedHashMap.put(d2, 15);
  linkedHashMap.put(d3, 5);
  linkedHashMap.put(d4, 20);
 
  for (Entry<Dog, Integer> entry : linkedHashMap.entrySet()) {
   System.out.println(entry.getKey() + " - " + entry.getValue());
  }  
 }
}
Output is:
red dog - 10
black dog - 15
white dog - 20
The difference is that if we use HashMap the output could be the following – the insertion order is not preserved.
red dog - 10
white dog - 20
black dog - 15

1 comment:

  1. Thank you for sharing wonderful information with us to get some idea about that content.
    Selenium Online Training
    Selenium Online Courses

    ReplyDelete

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...