-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashTableDemo.java
More file actions
56 lines (44 loc) · 1.66 KB
/
HashTableDemo.java
File metadata and controls
56 lines (44 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package learnCollections;
import java.util.Hashtable;
public class HashTableDemo {
public static void main(String[] args) {
Hashtable<Integer, String> hashtable = new Hashtable<>();
// Hashtable is synchronized
// no null key or value
// Legacy class, replaced by ConcurrentHashMap
// slower than HashMap
// only linked list in case of collision, no tree structure like in HashMap
// all methods are synchronized
hashtable.put(1, "One");
hashtable.put(2, "Two");
hashtable.put(3, "Three");
System.out.println(hashtable);
System.out.println(hashtable.get(2)); // Two
System.out.println(hashtable.containsKey(3)); // true
hashtable.remove(1);
System.out.println(hashtable); // {2=Two, 3=Three}
// hashtable.put(null, "value"); // throws NullPointerException
// hashtable.put(4, null); // throws NullPointerException
// HashMap<Integer, String> map = new HashMap<>();
Hashtable<Integer, String> map = new Hashtable<>();
Thread t1 = new Thread(() -> {
for(int i = 0; i < 1000; i++) {
map.put(i, "Thread 1");
}
});
Thread t2 = new Thread(() -> {
for(int i = 1000; i < 2000; i++) {
map.put(i, "Thread 2");
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final size: " + map.size()); // Final size: 2000, no data corruption due to synchronization
}
}