|
1 | | -``` HashTable v/s HashMap |
| 1 | +### HashTable v/s HashMap |
| 2 | + |
2 | 3 | Before this, I would suggest to go through the topic Iterator V/S Enumeration here.
|
3 | 4 |
|
4 | 5 | There are some significant factors that makes both of these data structures different
|
5 | 6 |
|
6 | | -1. Synchronization or Thread Safety |
7 | | -- This is the most important difference between the two. |
8 | | -- Hash tables are synchronized and thread safe whereas Hash maps are not. |
| 7 | +**1. Synchronization or Thread Safety** |
| 8 | +- This is the most important difference between the two. |
| 9 | +- Hash tables are synchronized and thread safe whereas Hash maps are not. |
9 | 10 |
|
10 | | -2. Null keys and Null values |
11 | | -- HashMap allows one null key and multiple null values, whereas hash table doesn't allow neither null values not null keys. |
| 11 | +**2. Null keys and Null values** |
| 12 | +- HashMap allows one null key and multiple null values, whereas hash table doesn't allow neither null values not null keys. |
12 | 13 |
|
13 | | -3. Iterating the Values |
14 | | -- Values in hash map are iterated using an iterator, whereas in hash table values are iterated using enumerator. |
15 | | -- Only Vector other then hash table uses enumerator to iterate through elements. |
| 14 | +**3. Iterating the Values** |
| 15 | +- Values in hash map are iterated using an iterator, whereas in hash table values are iterated using enumerator. |
| 16 | +- Only Vector other then hash table uses enumerator to iterate through elements. |
16 | 17 |
|
17 | | -4. Fail Fast Iterator |
18 | | -- Iterator in hash map is fail fast iterator, whereas enumerator for hash table is not. |
19 | | -- If hash table is structurally modified at any time after the iterator is created other then iterators own remove method, then it will throw Concurrent Modification exception. |
20 | | -- Structural modification means adding or removing elements from the collection. |
| 18 | +**4. Fail Fast Iterator** |
| 19 | +- Iterator in hash map is fail fast iterator, whereas enumerator for hash table is not. |
| 20 | +- If hash table is structurally modified at any time after the iterator is created other then iterators own remove method, then it will throw Concurrent Modification exception. |
| 21 | +- Structural modification means adding or removing elements from the collection. |
21 | 22 |
|
22 | | -5. Super class and Legacy |
23 | | -- HashTable is a subclass of Dictionary, which is now obsolete from JDK 1.7 |
| 23 | +**5. Super class and Legacy** |
| 24 | +- HashTable is a subclass of Dictionary, which is now obsolete from JDK 1.7 |
24 | 25 |
|
25 | | -6. Performance |
26 | | -- HashMap is much faster and uses less memory because it is not synchronized. |
| 26 | +**6. Performance** |
| 27 | +- HashMap is much faster and uses less memory because it is not synchronized. |
27 | 28 |
|
28 | | -Similarities : |
29 | | -1. Order of elements |
30 | | -- Cannot be guaranteed, because both of these work based on the hashing logic. use Linked Hash map for that |
31 | | -2. Both of them comes from Map interface. |
32 | | -3. Both provides constant time for performance for put and get methods, assuming that objects are distributed uniformly. |
33 | | -4. Both works on the principle of hashing |
| 29 | +**Similarities :** |
| 30 | +``` |
| 31 | +- Order of elements cannot be guaranteed, because both of these work based on the hashing logic. use Linked Hash map for that |
| 32 | +- Both of them comes from Map interface. |
| 33 | +- Both provides constant time for performance for put and get methods, assuming that objects are distributed uniformly. |
| 34 | +- Both works on the principle of hashing |
| 35 | +``` |
34 | 36 |
|
35 | | -When to use? |
| 37 | +**When to use?** |
| 38 | +``` |
36 | 39 | Avoid using HashTable, as they are obsolete now, ConcurrentHashMap has replaced it.
|
37 | 40 | Single threaded apps - use HashMap
|
38 | | - |
| 41 | +``` |
| 42 | + |
0 commit comments