-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHT.java
57 lines (45 loc) · 1.1 KB
/
DHT.java
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
import java.util.*;
import java.net.*;
import java.lang.*;
import java.io.*;
public class DHT implements Serializable {
Map<Long, String> map = new HashMap<>();
public long insert(String content) {
long key = (long) System.nanoTime();
map.put(key, content);
return key;
}
public String retrieve(long key) {
return map.get(key);
}
public void removeByKey(long key) {
map.remove(key);
}
public String getAllKeys() {
List<Long> keys = new ArrayList<Long>(map.keySet());
String stringified = "";
for (int i = 0; i < keys.size(); i++) {
if (i > 0) {
stringified += " ";
}
stringified += Long.toString(keys.get(i));
}
return stringified;
}
public void put(Long key, String value) {
map.put(key, value);
}
public int size() {
return map.size();
}
public boolean contains(Long key) {
return map.containsKey(key);
}
public void removeAll(DHT set) {
for ( Map.Entry<Long, String> entry : set.getTable().entrySet() )
map.remove(entry.getKey());
}
public Map<Long, String> getTable() {
return map;
}
}