2016-07-22

What is the difference between hashmap, hashtable and hashset

Hashmap:

  • Hash table implements Map interface
  • Hash table permits null values and the null key
  • HashMap class is equivalent to Hashtable, except that it is unsynchronized and permits nulls
  • It does not guarantee that the order will remain constant over time
  • constant-time performance for the operations (get, put and remove)
  •  Time complexity - O(1) for get, put and remove
                (Learn About capacity , load factor and fail-fast)
  • It is not synchronized
  • How to make Hashmap Synchronize
                 Map m = Collections.synchronizedMap(new HashMap(...));
  • Exapmle:
                   import java.util.*;
                   public class HashMapEx{


                   public static void main(String args[]) {
                  // Create a hash map
                     HashMap h = new HashMap();
                  // Put elements to the map
                     h.put(1,"x");
                     h.put(2,"y");
                     h.put(3,"z");

                    System.out.println(h.get("1"));
                     }
                   }
Hashtable:


No comments:

Post a Comment