Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Popular Java Map Implementations

The java.util.Map is an interface from the Collection API. It can grow its size. It is a collection where in a key maps a certain value. A key can only have one value. It does not allow duplicate keys. The most popular classes that implements the Map are java.util.HashTable, java.util.HashMap, java.util.TreeMap and java.util.LinkedHashMap. Let us look at the example code below and observe their similarities and differences.

  package com.example.core;

  import java.util.HashMap;
  import java.util.Hashtable;
  import java.util.LinkedHashMap;
  import java.util.TreeMap;

  public class HashTableVsHashMapVsTreeMap {

    public static void main(String[] args) {
      Hashtable table 
        = new Hashtable();
      table.put("1", "Hashtable value 1");
      table.put("2", "Hashtable value 2");
      //table.put(null, "Hashtable value 3");
      //table.put("3", null);
      for(String key : table.keySet()) {
        String value = table.get(key);
        System.out.println("key = " + key 
          +  " ... value = " + value);
      }
      /*
       * Hashtable is synchronized. It means, it is thread 
       * safe. This also means it is slower. Hashtable 
       * does not accept null keys or values. Hashtable 
       * does not guarantee the order of keys and values 
       * when iterating through it.
       */
      HashMap hmap 
        = new HashMap();
      hmap.put(1, "HashMap value 1");
      hmap.put(2, "HashMap value 2");
      hmap.put(null, "HashMap value 3");
      hmap.put(null, "HashMap value 4");
      hmap.put(3, null);
      hmap.put(4, null);
      for(Integer key : hmap.keySet()) {
        String value = hmap.get(key);
        System.out.println("key = " + key 
          +  " ... value = " + value);
      }
      /*
       * HashMap is not synchronized. It is not thread 
       * safe. It is fast compared to Hashtable. It 
       * accepts null key or null values. In this example, 
       * since the null value was used as a key twice,
       * the latest that was put into the map will prevail. 
       * You can put null values multiple times. HashMap 
       * does not guarantee the order of keys and values 
       * when iterating through it.
       */
      TreeMap tmap = new TreeMap();
      tmap.put(3, "TreeMap value 3");
      tmap.put(1, "TreeMap value 1");
      tmap.put(2, "TreeMap value 2");
      //tmap.put(null, "TreeMap value 3");
      //tmap.put(3, null);
      //tmap.put(4, null);
      for(Integer key : tmap.keySet()) {
        String value = tmap.get(key);
        System.out.println("key = " + key 
          +  " ... value = " + value);
      }
      /*
       * TreeMap is not synchronized. It is not thread 
       * safe. It is fast compared to Hashtable. It does 
       * not accept null keys and values. It guarantees 
       * the order of keys and values when iterating 
       * through it. It uses the natural order of keys. 
       * In this case, in ascending order of 1,2 and 3.
       */
      LinkedHashMap lhmap 
        = new LinkedHashMap();
      lhmap.put(3, "LinkedHashMap value 3");
      lhmap.put(1, "LinkedHashMap value 1");
      lhmap.put(2, "LinkedHashMap value 2");
      //lhmap.put(null, "LinkedHashMap value 3");
      //lhmap.put(3, null);
      //lhmap.put(4, null);
      for(Integer key : lhmap.keySet()) {
        String value = lhmap.get(key);
        System.out.println("key = " + key 
          +  " ... value = " + value);
      }
      /*
       * LinkedHashMap is not synchronized. It is not thread safe.
       * It is fast compared to Hashtable. It does not accept
       * null keys and values. It guarantees the order of keys
       * and values when iterating through it. It uses the order
       * of insertion.
       */
    }
  }
                            



Back    Next