- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the differences between HashMap and HashTable in Java
HashMap and HashTable both are one of the most important classes of Java Collection framework. Both HashMap and HashTable stores the data in key value pair and at the time storing data hashing is used to hash the key and the resulting hash code is used as the index at which the value is stored within the table. But still, there are many differences between both these classes which we would discuss below.
The following are the important differences between HashMap and HashTable.
Sr. No. | Key | HashMap | HashTable |
---|---|---|---|
1 | Introduction | Hashmap is the advanced version of HashTable and is introduced as a new class in JDK 1.2. | HashTable on the other hand is the legacy class and was introduced prior to HashMap. |
2 | Internal implementation | Internal implementation of both classes is same upto some extent but in case of HashMap one null key and multiple null values are allowed. | HashTable internally implemented in such manner that it does not allow any null key or any null value. |
3 | Synchronization | Synchronization is not implemented in HashMap and is not thread safe so can't be shared between many threads without proper synchronization code. | On the other hand, HashTable is synchronized and is thread safe so can be shared with many threads. |
4 | Traversed the elements | HashMap provides Iterator for its iteration in order to traverse the values stored in it. | On the other hand along with Iterator HashTable also provides Enumerator to traverse the values stored in it. |
5 | Index performance | Due to the absence of synchronization HashMap is faster as compare to HashTable and is preferred when synchronization is not needed. | Synchronization in HashTable makes it slower as compare to HashMap but also eliminate the writing of extra code to obtain the synchronization. |
6 | Inheritance | HashMap inherits AbstractMap class. | On the other hand, HashTable inherits Dictionary class. |
Example of HashMap vs HashTable
JavaTester.java
import java.util.*; import java.lang.*; import java.io.*; public class JavaTester{ public static void main(String args[]){ Hashtable ht=new Hashtable(); ht.put(101,"John"); ht.put(101,"Jhony"); ht.put(102,"Smith"); ht.put(103,"Andy"); System.out.println("-------------Hash table--------------"); Set<Integer> keySet = ht.keySet(); for (Integer key:keySet) { System.out.println(key + " "+ht.get(key)); } HashMap hm=new HashMap(); hm.put(100,"John"); hm.put(104,"John"); // hash map allows duplicate values hm.put(101,"Smith"); hm.put(102,"Andy"); System.out.println("-----------Hash map-----------"); Set<Integer> keySet1 = ht.keySet(); for (Integer key:keySet) { System.out.println(key + " "+hm.get(key)); } } }
Output
-------------Hash table-------------- 103 Andy 102 Smith 101 Jhony -----------Hash map----------- 100 John 101 Smith 102 Andy 104 John
- Related Articles
- Differences between HashMap and Hashtable in Java
- Difference between HashMap and HashTable in Java.
- Difference between HashTable and HashMap in Java
- What is the differences between TreeMap, HashMap and LinkedHashMap in Java?
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- Difference between HashMap and ConcurrentHashMap in Java
- Difference between HashMap and HashSet in Java.
- Difference between EnumMap and HashMap in Java
- Difference between HashTable and ConcurrentHashMap in Java
- Difference Between HashMap and TreeMap in Java
- Difference Between HashMap and LinkedHashMap in Java
- What is the difference between Dictionary and HashTable in PowerShell?
- Difference between TreeMap, HashMap, and LinkedHashMap in Java
- What are the differences between C++ and Java?
- What are the differences between C and Java?

Advertisements