forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentHashMap.java
More file actions
189 lines (173 loc) · 5.54 KB
/
ConcurrentHashMap.java
File metadata and controls
189 lines (173 loc) · 5.54 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.thealgorithms.datastructures.hashmap.hashing;
import java.util.concurrent.locks.ReentrantLock;
/**
* A thread-safe implementation of a HashMap using separate chaining with linked lists
* and ReentrantLocks for concurrency control.
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*/
@SuppressWarnings("rawtypes")
public class ConcurrentHashMap<K, V> {
private final int hashSize;
private final Bucket<K, V>[] buckets;
private final ReentrantLock[] locks;
/**
* Constructs a ConcurrentHashMap with the specified hash size.
*
* @param hashSize the number of buckets in the hash map
*/
@SuppressWarnings("unchecked")
public ConcurrentHashMap(int hashSize) {
this.hashSize = hashSize;
this.buckets = new Bucket[hashSize];
this.locks = new ReentrantLock[hashSize];
for (int i = 0; i < hashSize; i++) {
buckets[i] = new Bucket<>();
locks[i] = new ReentrantLock();
}
}
/**
* Computes the hash code for the specified key.
* Null keys are hashed to bucket 0.
*
* @param key the key for which the hash code is to be computed
* @return the hash code corresponding to the key
*/
private int computeHash(K key) {
if (key == null) {
return 0; // Use a special bucket (e.g., bucket 0) for null keys
}
int hash = key.hashCode() % hashSize;
return hash < 0 ? hash + hashSize : hash;
}
/**
* Inserts the specified key-value pair into the hash map.
* If the key already exists, the value is updated.
*
* @param key the key to be inserted
* @param value the value to be associated with the key
*/
public void put(K key, V value) {
int hash = computeHash(key);
locks[hash].lock();
try {
buckets[hash].put(key, value);
} finally {
locks[hash].unlock();
}
}
/**
* Retrieves the value associated with the specified key.
*
* @param key the key whose associated value is to be returned
* @return the value associated with the specified key, or null if the key does not exist
*/
public V get(K key) {
int hash = computeHash(key);
locks[hash].lock();
try {
return buckets[hash].get(key);
} finally {
locks[hash].unlock();
}
}
/**
* Removes the key-value pair associated with the specified key from the hash map.
*
* @param key the key whose key-value pair is to be removed
*/
public void remove(K key) {
int hash = computeHash(key);
locks[hash].lock();
try {
buckets[hash].remove(key);
} finally {
locks[hash].unlock();
}
}
/**
* Checks if the hash map contains the specified key.
*
* @param key the key to check
* @return true if the key exists, false otherwise
*/
public boolean containsKey(K key) {
int hash = computeHash(key);
locks[hash].lock();
try {
return buckets[hash].containsKey(key);
} finally {
locks[hash].unlock();
}
}
/**
* A nested static class representing a bucket in the hash map.
* Each bucket uses a linked list to store key-value pairs.
*
* @param <K> the type of keys maintained by this bucket
* @param <V> the type of mapped values
*/
private static class Bucket<K, V> {
private Node<K, V> head;
public void put(K key, V value) {
Node<K, V> node = findNode(key);
if (node != null) {
node.value = value;
} else {
Node<K, V> newNode = new Node<>(key, value);
newNode.next = head;
head = newNode;
}
}
public V get(K key) {
Node<K, V> node = findNode(key);
return node != null ? node.value : null;
}
public void remove(K key) {
if (head == null) {
return;
}
if ((key == null && head.key == null) || (head.key != null && head.key.equals(key))) {
head = head.next;
return;
}
Node<K, V> current = head;
while (current.next != null) {
if ((key == null && current.next.key == null) || (current.next.key != null && current.next.key.equals(key))) {
current.next = current.next.next;
return;
}
current = current.next;
}
}
public boolean containsKey(K key) {
return findNode(key) != null;
}
private Node<K, V> findNode(K key) {
Node<K, V> current = head;
while (current != null) {
if ((key == null && current.key == null) || (current.key != null && current.key.equals(key))) {
return current;
}
current = current.next;
}
return null;
}
}
/**
* A nested static class representing a node in the linked list.
*
* @param <K> the type of key maintained by this node
* @param <V> the type of value maintained by this node
*/
private static class Node<K, V> {
private final K key;
private V value;
private Node<K, V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
}