Allison Ziegler před 6 roky
rodič
revize
f16c5ad803

+ 47
- 8
src/main/java/org/zipcoder/store/MyHashMap.java Zobrazit soubor

@@ -4,7 +4,7 @@ import java.util.*;
4 4
 
5 5
 public class MyHashMap implements MyMap{
6 6
     // final field that doesn't change for any object
7
-    private static final int BUCKET_SIZE = 15;
7
+    private static final int BUCKET_SIZE = 100;
8 8
 
9 9
     // instance field that can only be set once. It is different for every Object
10 10
     private final List<Entry>[] entries;
@@ -19,40 +19,79 @@ public class MyHashMap implements MyMap{
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int count = 0;
23
+        for (List<Entry> list : entries) {
24
+            count += list.size();
25
+        }
26
+        return count;
23 27
     }
24 28
 
25 29
     @Override
26 30
     public boolean isEmptry() {
27
-        return false;
31
+        if (size() == 0) return true;
32
+        else return false;
28 33
     }
29 34
 
30 35
     @Override
31 36
     public Cart get(User key) {
32
-        return null;
37
+        List<Entry> bucket = entries[bucketIndex(key)];
38
+        int index = keyIndex(bucket, key);
39
+        if (index >= 0 ) return bucket.get(keyIndex(bucket, key)).getValue();
40
+        else return null;
33 41
     }
34 42
 
35 43
     @Override
36 44
     public void put(User key, Cart value) {
37
-
45
+        entries[bucketIndex(key)].add(new Entry(key, value));
38 46
     }
39 47
 
40 48
     @Override
41 49
     public List<User> getKeys() {
42
-        return null;
50
+        List<User> keys = new ArrayList<User>();
51
+        for (List<Entry> list : entries) {
52
+            for (Entry e : list) {
53
+                keys.add(e.getKey());
54
+            }
55
+        }
56
+        return keys;
43 57
     }
44 58
 
45 59
     @Override
46 60
     public List<Cart> getValues() {
47
-        return null;
61
+        List<Cart> values = new ArrayList<Cart>();
62
+        for (List<Entry> list : entries) {
63
+            for (Entry e : list) {
64
+                values.add(e.getValue());
65
+            }
66
+        }
67
+        return values;
48 68
     }
49 69
 
50 70
     @Override
51 71
     public Cart remove(User key) {
52
-        return null;
72
+        List<Entry> bucket = entries[bucketIndex(key)];
73
+        Iterator<Entry> iterator = bucket.iterator();
74
+        Cart removed = null;
75
+        while(iterator.hasNext()) {
76
+            Entry entry = iterator.next();
77
+            if (entry.getKey().equals(key)) {
78
+                removed = entry.getValue();
79
+                iterator.remove();
80
+            }
81
+        }
82
+        return removed;
53 83
     }
54 84
 
55 85
     private int bucketIndex(User key){
56 86
         return Math.abs(key.hashCode()) % entries.length;
57 87
     }
88
+    private int keyIndex(List<Entry> bucket, User key) {
89
+        int index = -1;
90
+        for (int i = 0; i < bucket.size(); i++) {
91
+            if (bucket.get(i).getKey().equals(key)) {
92
+                index = i;
93
+            }
94
+        }
95
+        return index;
96
+    }
58 97
 }

+ 52
- 52
src/test/java/org/zipcoder/store/MyHashMapTest.java Zobrazit soubor

@@ -175,56 +175,56 @@ public class MyHashMapTest {
175 175
     }
176 176
 
177 177
 //    I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178
-//    @Test
179
-//    public void testPutAndGet_WithManyItems(){
180
-//        // Given
181
-//        int numberOfEntries = 70000;
182
-//
183
-//        for (int i = 0; i < numberOfEntries; i++) {
184
-//            myMap.put(new User(i), new Cart(i));
185
-//        }
186
-//
187
-//
188
-//        for (int i = 0; i < numberOfEntries; i++) {
189
-//            // When
190
-//            Cart actualCart = myMap.get(new User(i));
191
-//            Cart expectedCart = new Cart(i);
192
-//
193
-//            // Then
194
-//            Assert.assertEquals(actualCart, expectedCart);
195
-//        }
196
-//    }
197
-
198
-//    @Test
199
-//    public void testRemove(){
200
-//        // Given
201
-//        User user1 = new User("Lena");
202
-//        Cart cart1 = new Cart(11);
203
-//
204
-//        User user2 = new User("Jamal");
205
-//        Cart cart2 = new Cart(21);
206
-//
207
-//        User user3 = new User("Kelvin");
208
-//        Cart cart3 = new Cart(31);
209
-//
210
-//        // When
211
-//        myMap.put(user1, cart1);
212
-//        myMap.put(user2, cart2);
213
-//        myMap.put(user3, cart3);
214
-//
215
-//        // Then
216
-//        Cart actualCart = myMap.remove(user2);
217
-//
218
-//        Assert.assertEquals(cart2, actualCart);
219
-//        Assert.assertNull(myMap.get(user2));
220
-//    }
221
-//
222
-//    @Test
223
-//    public void testRemove_CartNotInTheMap(){
224
-//        // Given
225
-//        User user1 = new User("Lena");
226
-//
227
-//        // When & Then
228
-//        Assert.assertNull(myMap.remove(user1));
229
-//    }
178
+    @Test
179
+    public void testPutAndGet_WithManyItems(){
180
+        // Given
181
+        int numberOfEntries = 70000;
182
+
183
+        for (int i = 0; i < numberOfEntries; i++) {
184
+            myMap.put(new User(i), new Cart(i));
185
+        }
186
+
187
+
188
+        for (int i = 0; i < numberOfEntries; i++) {
189
+            // When
190
+            Cart actualCart = myMap.get(new User(i));
191
+            Cart expectedCart = new Cart(i);
192
+
193
+            // Then
194
+            Assert.assertEquals(actualCart, expectedCart);
195
+        }
196
+    }
197
+
198
+    @Test
199
+    public void testRemove(){
200
+        // Given
201
+        User user1 = new User("Lena");
202
+        Cart cart1 = new Cart(11);
203
+
204
+        User user2 = new User("Jamal");
205
+        Cart cart2 = new Cart(21);
206
+
207
+        User user3 = new User("Kelvin");
208
+        Cart cart3 = new Cart(31);
209
+
210
+        // When
211
+        myMap.put(user1, cart1);
212
+        myMap.put(user2, cart2);
213
+        myMap.put(user3, cart3);
214
+
215
+        // Then
216
+        Cart actualCart = myMap.remove(user2);
217
+
218
+        Assert.assertEquals(cart2, actualCart);
219
+        Assert.assertNull(myMap.get(user2));
220
+    }
221
+
222
+    @Test
223
+    public void testRemove_CartNotInTheMap(){
224
+        // Given
225
+        User user1 = new User("Lena");
226
+
227
+        // When & Then
228
+        Assert.assertNull(myMap.remove(user1));
229
+    }
230 230
 }