Lauren Green пре 6 година
родитељ
комит
c85327dd01

+ 57
- 5
src/main/java/org/zipcoder/store/MyHashMap.java Прегледај датотеку

@@ -17,41 +17,93 @@ public class MyHashMap implements MyMap{
17 17
         }
18 18
     }
19 19
 
20
+    public List<Entry>[] getEntries() {
21
+        return entries;
22
+    }
23
+
20 24
     @Override
21 25
     public int size() {
22
-        return -1;
26
+        int count = 0;
27
+        for(int i = 0; i < BUCKET_SIZE; i++) {
28
+            if(!getEntries()[i].isEmpty()) {
29
+                count++;
30
+            }
31
+        }
32
+        return count;
23 33
     }
24 34
 
25 35
     @Override
26 36
     public boolean isEmpty() {
37
+
38
+        if(size() == 0) {
39
+            return true;
40
+        }
41
+
27 42
         return false;
28 43
     }
29 44
 
30 45
     @Override
31 46
     public Cart get(User key) {
47
+        for(Entry e: getEntries()[bucketIndex(key)]) {
48
+            if(e.getKey().equals(key)){
49
+                return e.getValue();
50
+            }
51
+        }
52
+
32 53
         return null;
33 54
     }
34 55
 
35 56
     @Override
36 57
     public void put(User key, Cart value) {
58
+        if(get(key) != null) {
59
+            for (Entry e : getEntries()[bucketIndex(key)]) {
60
+                if (e.getKey().equals(key)) {
61
+                    e.setValue(value);
62
+                }
63
+            }
64
+        } else {
65
+
66
+            Entry entry = new Entry(key, value);
67
+            getEntries()[bucketIndex(key)].add(entry);
37 68
 
69
+        }
38 70
     }
39 71
 
40 72
     @Override
41 73
     public List<User> getKeys() {
42
-        return null;
74
+        List<User> keys = new ArrayList<>();
75
+        for (int i = 0; i < BUCKET_SIZE; i++) {
76
+            for (Entry e : getEntries()[i]) {
77
+                keys.add(e.getKey());
78
+            }
79
+        }
80
+        return keys;
43 81
     }
44 82
 
45 83
     @Override
46 84
     public List<Cart> getValues() {
47
-        return null;
85
+        List<Cart> values = new ArrayList<>();
86
+        for (int i = 0; i < BUCKET_SIZE; i++) {
87
+            for (Entry e : getEntries()[i]) {
88
+                values.add(e.getValue());
89
+            }
90
+        }
91
+        return values;
48 92
     }
49 93
 
50 94
     @Override
51 95
     public Cart remove(User key) {
52
-        return null;
53
-    }
96
+        for (int i = 0; i < BUCKET_SIZE; i++) {
97
+            for (Entry e : getEntries()[i]) {
98
+                if (e.getKey() == key) {
99
+                    getEntries()[i].remove(e);
100
+                    return e.getValue();
101
+                }
102
+            }
103
+        }
104
+            return null;
54 105
 
106
+    }
55 107
     private int bucketIndex(User key){
56 108
         return Math.abs(key.hashCode()) % entries.length;
57 109
     }

+ 52
- 52
src/test/java/org/zipcoder/store/MyHashMapTest.java Прегледај датотеку

@@ -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
 }