Repository navigation
Expand file tree
/
Copy pathB2.java
More file actions
74 lines (57 loc) · 1.67 KB
/
Copy pathB2.java
File metadata and controls
74 lines (57 loc) · 1.67 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
import java.util.*;
public class B2 {
public static class RandomHashMap<K, V> {
private Map<K, V> hm;
private List<K> keys;
private Random generator;
public RandomHashMap() {
hm = new HashMap<K, V>();
keys = new ArrayList<K>();
generator = new Random();
}
public V get(K key) {
return hm.get(key);
}
public void put(K key, V value) {
if (hm.get(key) == null) {
keys.add(key);
}
hm.put(key, value);
}
public boolean remove(K key) {
if (hm.get(key) == null) {
return false;
}
for (int i = 0; i < keys.size(); i++) {
if (keys.get(i).equals(key)) {
keys.remove(i);
}
}
hm.remove(key);
return true;
}
public K getRandomKey() {
if (hm.size() == 0) {
return null;
}
int idx = generator.nextInt(hm.size());
return keys.get(idx);
}
}
public static void main(String[] args) {
RandomHashMap<String, Integer> hm = new RandomHashMap<>();
System.out.println(hm.getRandomKey());
hm.put("A", 1);
hm.put("B", 2);
hm.put("C", 3);
hm.put("D", 4);
for (int i = 0; i < 5; i++) {
System.out.println(hm.getRandomKey());
}
System.out.println(hm.get("C"));
System.out.println(hm.get("A"));
System.out.println(hm.remove("A"));
System.out.println(hm.remove("Null"));
return;
}
}