-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathMyBetterMap.java
More file actions
156 lines (136 loc) · 3.09 KB
/
Copy pathMyBetterMap.java
File metadata and controls
156 lines (136 loc) · 3.09 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
package com.allendowney.thinkdast;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Implementation of a Map using a collection of MyLinearMap, and
* using `hashCode` to determine which map each key should go in.
*
* @author downey
* @param <K>
* @param <V>
*
*/
public class MyBetterMap<K, V> implements Map<K, V> {
// MyBetterMap uses a collection of MyLinearMap
protected List<MyLinearMap<K, V>> maps;
/**
* Initialize the map with 2 sub-maps.
*
*/
public MyBetterMap() {
makeMaps(2);
}
/**
* Makes a collection of `k` MyLinearMap
*
* @param k
*/
protected void makeMaps(int k) {
maps = new ArrayList<MyLinearMap<K, V>>(k);
for (int i=0; i<k; i++) {
maps.add(new MyLinearMap<K, V>());
}
}
@Override
public void clear() {
// clear the sub-maps
for (int i=0; i<maps.size(); i++) {
maps.get(i).clear();
}
}
/**
* Uses the hashCode to find the map that would/should contain the given key.
*
* @param key
* @return
*/
protected MyLinearMap<K, V> chooseMap(Object key) {
int index = key==null ? 0 : Math.abs(key.hashCode()) % maps.size();
return maps.get(index);
}
@Override
public boolean containsKey(Object target) {
// to find a key, we only have to search one map
// TODO: FILL THIS IN!
return false;
}
@Override
public boolean containsValue(Object target) {
// to find a value, we have to search all map
// TODO: FILL THIS IN!
return false;
}
@Override
public Set<Map.Entry<K, V>> entrySet() {
throw new UnsupportedOperationException();
}
@Override
public V get(Object key) {
MyLinearMap<K, V> map = chooseMap(key);
return map.get(key);
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public Set<K> keySet() {
// add up the keySets from the sub-maps
Set<K> set = new HashSet<K>();
for (MyLinearMap<K, V> map: maps) {
set.addAll(map.keySet());
}
return set;
}
@Override
public V put(K key, V value) {
MyLinearMap<K, V> map = chooseMap(key);
return map.put(key, value);
}
@Override
public void putAll(Map<? extends K, ? extends V> map) {
for (Map.Entry<? extends K, ? extends V> entry: map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public V remove(Object key) {
MyLinearMap<K, V> map = chooseMap(key);
return map.remove(key);
}
@Override
public int size() {
// add up the sizes of the sub-maps
int total = 0;
for (MyLinearMap<K, V> map: maps) {
total += map.size();
}
return total;
}
@Override
public Collection<V> values() {
// add up the valueSets from the sub-maps
Set<V> set = new HashSet<V>();
for (MyLinearMap<K, V> map: maps) {
set.addAll(map.values());
}
return set;
}
/**
* @param args
*/
public static void main(String[] args) {
Map<String, Integer> map = new MyBetterMap<String, Integer>();
map.put("Word1", 1);
map.put("Word2", 2);
Integer value = map.get("Word1");
System.out.println(value);
for (String key: map.keySet()) {
System.out.println(key + ", " + map.get(key));
}
}
}