Saturday, 18 May 2013

Interview Question #12 Differences between HashMap and Hashtable?

There are several differences between HashMap and Hashtable. Hashtable was introduced earlier whereas HashMap is a newly introduced as a part of Java Collection Framework.

  • Hashtable is synchronized whereas HashMap is not.This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
  • Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  • 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.
If synchronization is not an issue for you HashMap is recommended. If synchronization becomes an issue, you may also look at ConcurrentHashMap.

Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top