11. What is difference between HashMap and HashTable? - TopicsExpress



          

11. What is difference between HashMap and HashTable? ------------------------------------------------------------------------------- Hashtable and HashMap are two hash based collection in Java and works on principle of hashing. Both store data in key/value pairs. Both HashMap and HashTable implements Map interface. Hashtable introduced with JDK1.0 whereas HashMap introduced with JDK1.2 Similarities: -------------------------- 1) Both Hashtable and HashMap implements java.util.Map interface. 2) Hashtable and HashMap both are hash based collection and works on principle of hashing. 3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket. 4) From JDK 4 both Hashtable and HashMap are part of Java collection framework. Differences: ------------------------------- 1. (Synchronization) By default, HashMap is not synchronized whereas HashTable is synchronized, which means Hashtable is thread-safe and can be shared between multiple threads but HashMap can not be shared between multiple threads without proper synchronization. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones. But we can make HashMap as synchronized by using Collections class Example : HashMap map = new HashMap(); Collections.synchronizedMap(map); 2. (Null key and value) Hashtable does not allow null keys or values but HashMap allows one null key and any number of null values. 3. (Order) HashMap does not guarantee that the order of the map will remain constant over time. 4. (Fail fast) In HashTable, Enumeration is not fail-safe. It allows you to change the Hashtable content while traversing. In HashMap, Iterator is fail-safe, because it won’t allow you to change the Map while doing iteration. Fail safe → Fail-fast means when you try to modify the content when you are iterating, it will throw ConcurrentModificationException and fail immediately. 5. (Performance) Another difference is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment. 6. In HashTable, each time a thread is required to acquire a lock before using it but in HashMap, lock is not required by threads. 7. (extends) HashTable extends Dictionary class where as HashMap extends AbstractMap class 8. The operations like insertion / deletion and modification are taking more amount of time i.e, more expensive. 9. Hashtable is serialized but HashMap is not serialized. 10. Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java. Example : import java.util.*; public class HashtableExample { public static void main (String[] args) { Hashtable names = new Hashtable(); names.put(1,ranga); names.put(2,reddy); //names.put(3,null); //throws NullPointerException Enumeration enames = names.keys(); names.put(3, rangareddy); //allows it. doesnt throw any exception while(enames.hasMoreElements()){ Integer key = (Integer)enames.nextElement(); System.out.println(key + : + names.get(key)); } } } Output: 3: rangareddy 2: reddy 1: ranga import java.util.*; class HashMapExample { public static void main (String[] args) { HashMap names = new HashMap(); names.put(1, ranga); names.put(2, reddy); names.put(null, null); // allows null values Set keys = names.keySet(); Iterator it = keys.iterator(); //names.put(3,rangareddy); //throws ConcurrentModificationException while(it.hasNext()){ Integer key = (Integer)it.next(); System.out.println(key +: + names.get(key)); } } } Output: null: null 1: ranga 2: reddy
Posted on: Wed, 09 Jul 2014 11:31:39 +0000

Recently Viewed Topics




© 2015