What is the difference between HashMap and Hashtable in Java?
Hi everyone! I've just started learning the Java Collections Framework and got confused about two similar classes — HashMap and Hashtable. They both implement the Map interface and seem to do the same thing: store key-value pairs. But I've heard there are important differences between them, and Hashtable is considered obsolete.
I wrote a small test code but don't see the difference:
```java
import java.util.*;
public class TestMap {
public static void main(String[] args) {
// HashMap
Map hashMap = new HashMap<>();
hashMap.put("key1", "value1");
hashMap.put(null, "nullValue"); // works
System.out.println("HashMap: " + hashMap);
// Hashtable
Map hashtable = new Hashtable<>();
hashtable.put("key1", "value
No answers yet.
Log in to leave an answer
Blogs