HashMap vs. HashTable

When come to a time you need to choose from HashMap and HashTable, here is the differences.

1. HashMap is faster in single thread environment because it is not thread safe. On the other hand HashTable is thread safe, but performs slower than HashMap in single thread environment. Therefore for technical questions sake, mostly it is wise to choose HashMap.

2. HashMap allows null values whereas HashTable does not. Therefore if want to use a HashMap to check if a key has a correspondence, it will simply return null if there is no such correspondence.

3. “The third significant difference between HashMap vs HashTable is that Iterator in the HashMap is a fail-fast iterator 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. ” – quote from MANISH CHHABRA’s blog. Therefore if in the case wanting to predict iteration order, HashMap can be swapped by one of its subclass LinkedHashMap.

Basically, unless synchronization is a issue, normally HashMap is a wiser choice.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s