Eric Foster пре 6 година
родитељ
комит
68da67f480

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

@@ -32,31 +32,59 @@ public class MyHashMap implements MyMap{
32 32
 
33 33
     @Override
34 34
     public Cart get(User key) {
35
-        for(int i = 0; i < BUCKET_SIZE; i++) {
36
-            entries[i] = new ArrayList<>();
35
+        int bucket = bucketIndex(key);
36
+        Iterator<Entry> entryIterator = entries[bucket].iterator();
37
+        Entry retrievedEntry;
38
+        Cart retrievedCart = null;
39
+        while (entryIterator.hasNext()){
40
+            retrievedEntry = entryIterator.next();
41
+            if (retrievedEntry.getKey().equals(key)){
42
+                retrievedCart = retrievedEntry.getValue();
43
+            }
37 44
         }
38
-        return null;
45
+        return retrievedCart;
39 46
     }
40 47
 
41 48
     @Override
42 49
     public void put(User key, Cart value) {
50
+        int bucket = bucketIndex(key);
43 51
         Entry newEntry = new Entry(key, value);
44
-        entries[0].add(newEntry);
52
+        entries[bucket].add(newEntry);
45 53
     }
46 54
 
47 55
     @Override
48 56
     public List<User> getKeys() {
49
-        return null;
57
+        List<User> entriesUser = new ArrayList<>();
58
+        for (int i = 0; i < BUCKET_SIZE; i++) {
59
+            for (Entry e : entries[i]) {
60
+                entriesUser.add(e.getKey());
61
+            }
62
+        }
63
+        return entriesUser;
50 64
     }
51 65
 
52 66
     @Override
53 67
     public List<Cart> getValues() {
54
-        return null;
68
+        List<Cart> entriesCart = new ArrayList<>();
69
+        for (int i = 0; i < BUCKET_SIZE; i++) {
70
+            for (Entry e : entries[i]) {
71
+                entriesCart.add(e.getValue());
72
+            }
73
+        }
74
+        return entriesCart;
55 75
     }
56 76
 
57 77
     @Override
58 78
     public Cart remove(User key) {
59
-        return null;
79
+        int bucket = bucketIndex(key);
80
+        Cart removedCart = null;
81
+        for (int i=0; i<entries[bucket].size(); i++){
82
+            if (entries[bucket].get(i).getKey() == key){
83
+                removedCart = entries[bucket].get(i).getValue();
84
+                entries[bucket].remove(entries[bucket].get(i));
85
+            }
86
+        }
87
+        return removedCart;
60 88
     }
61 89
 
62 90
     private int bucketIndex(User key){

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