-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMyMap.java
More file actions
62 lines (49 loc) · 1.31 KB
/
MyMap.java
File metadata and controls
62 lines (49 loc) · 1.31 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
public class MyMap<K, V> {
private MyArrayList<Entry> map;
public MyMap() {
map = new MyArrayList<>();
}
public boolean containsKey(K key) {
for (int i = 0; i < this.map.size(); i++) {
if (map.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}
public boolean containsValue(V value) {
for (int i = 0; i < this.map.size(); i++) {
if (map.get(i).getValue().equals(value)) {
return true;
}
}
return false;
}
public void put(K key, V value) {
for (int i = 0; i < map.size(); i++) {
if (containsKey(key)) {
map.set(i, new Entry<>(key, value));
return;
}
}
map.add(new Entry<>(key, value));
}
public V get(K key) {
for (int i = 0; i < map.size(); i++) {
if (map.get(i).getKey().equals(key)) {
return (V)map.get(i).getValue();
}
}
return null;
}
public void remove(K key) {
for (int i = 0; i < map.size(); i++) {
if (map.get(i).getKey().equals(key)) {
map.remove(i);
}
}
}
public Integer size() {
return map.size();
}
}