Monday 28 December 2015

Difference between HashMap and TreeMap with an Example

In this post, I am going to explain the differences between HashMap and TreeMap with an example. Difference between HashMap and TreeMap is one of the interview question, you must prepare before attending.

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.

Roll Number
Name
123
Krishna
124
Rama
125
Anil
126
Kiran

In the above table, Roll number is the key, which maps to student name. I.e, Roll Number 123 maps to the student name Krishna, 126 maps to the student name Kiran.

HashMap, TreeMap, and LinkedHashMap are the General Implementations of the Map interface.

Following are notable differences between HashMap and TreeMap.

a. Ordering
HashMap don’t maintain any order while storing elements, where as TreeMap store elements according to their natural ordering.
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapEx {
 private static void displayMap(Map<String, String> map) {
  Set<String> keys = map.keySet();

  for (String country : keys) {
   System.out.println(country + " : " + map.get(country));
  }
 }

 public static void main(String args[]) {
  Map<String, String> countryHashMap = new HashMap<>();
  Map<String, String> countryTreeMap = new TreeMap<>();

  countryHashMap.put("Malaysia", "Kuala Lumpur");
  countryHashMap.put("Namibia", "Windhoek");
  countryHashMap.put("United States of America", "Washington, D.C.");
  countryHashMap.put("India", "New Delhi");
  countryHashMap.put("Lebanon", "Beirut");

  countryTreeMap.put("Malaysia", "Kuala Lumpur");
  countryTreeMap.put("Namibia", "Windhoek");
  countryTreeMap.put("United States of America", "Washington, D.C.");
  countryTreeMap.put("India", "New Delhi");
  countryTreeMap.put("Lebanon", "Beirut");

  System.out.println("Data in HashMap");
  System.out.println("********************");
  displayMap(countryHashMap);

  System.out.println("\nData in TreeMap");
  System.out.println("********************");
  displayMap(countryTreeMap);
 }
}


Output
Data in HashMap
********************
Lebanon : Beirut
Namibia : Windhoek
United States of America : Washington, D.C.
Malaysia : Kuala Lumpur
India : New Delhi

Data in TreeMap
********************
India : New Delhi
Lebanon : Beirut
Malaysia : Kuala Lumpur
Namibia : Windhoek
United States of America : Washington, D.C.


As you observe the output, countryTreeMap display the elements in the Ascending order of their keys, where as countryHashMap don't maintain any order.

b. Storing of elements
TreeMap internally uses Red-Black tree implementation to store the elements, where as HashMap store elements based on the hash value of keys.

c. Performance
HashMap perform lookup(search) operation in O(1) time where as TreeMap perform in O(log n) time.

d. TreeMap doesn't support null keys, Where as HashMap supports null key.

e. HashMap implements Map interface, where as TreeMap implements NavigableMap interface.

f. TreeMap provides following extra functionality, which are not provided by HashMap.

Function
Description
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
Get the First Entry in TreeMap
Get the First Key currently in the map
Get the Floor Entry of this key
Get the Floor key specific to this key
Get sorted map whose keys are strictly less than toKey
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
Get the Mapping with least key strictly greater than the given key
Returns the least key strictly greater than the given key, or null if there is no such key.
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
Returns the last (highest) key currently in this map.
Get mapping associated with the greatest key strictly less than this key
Get greatest key strictly less than given key
Returns a view of the portion of this map whose keys range from fromKey to toKey.

Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.


You may like



                                                 Home

No comments:

Post a Comment