Hashmap vs Hashtable - Part 2

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile.

5 differences between Hashmap vs Hashtable with explanation?

There are several differences between HashMap and Hashtable in Java:
1.     Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
2.     HashMap does not guarantee that the order of the map will remain constant over time and HashMap is much faster uses less memory than
Hashtable because former is unsynchronized.
3.     Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
4.     One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
5.     Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.

HashMap can be synchronized by
Map m = Collections.synchronizeMap(hashMap);

If you still have any question you can reach me any time through email (jigyasu2010@hotmail.com)

Comments

Popular posts from this blog

How Method Overloading Helping in Java - Part 9

Important Java Interview Questions - Part 7

Access Modifiers in java