Map v/s Table |
HashMap always brings innumerable interview questions along with itself. One of the common interview questions is the difference and similarities between Java HashMap and HashTable. You might not get the direct question like this but something on similar lines.
We have tried to list the difference and similarity. Have a go!
Differences
|
||
S No.
|
HashMap
|
HashTable
|
1
|
Not synchronized. Not thread safe
|
Synchronized. Thread safe
|
2
|
Allows one Null Key and multiple
null values
|
Null keys and Null values not
allowed
|
3
|
Iterated by using Iterator
|
Iterated by using Enumerator
|
4
|
Iterator in HashMap is fail-fast
|
Enumerator in HashTable is not
fail fast
|
5
|
Faster and uses less memory as unsynchronized
|
Slower and uses more memory as synchronized
|
Similarities
|
||
S No.
|
HashMap
|
HashTable
|
1
|
Does not guarantee order of Map
|
Does not guarantee order of Map
|
2
|
Constant Time for put/get
|
Constant Time for put/get
|
3
|
Works on Hashing Principle
|
Works on Hashing Principle
|
4
|
Implements Map Interface
|
Implements Map Interface
|
No comments:
Post a Comment