Trinh Tong 6 年 前
コミット
52a7e9eee8
共有2 個のファイルを変更した88 個の追加58 個の削除を含む
  1. 36
    6
      src/main/java/org/zipcoder/store/MyHashMap.java
  2. 52
    52
      src/test/java/org/zipcoder/store/MyHashMapTest.java

+ 36
- 6
src/main/java/org/zipcoder/store/MyHashMap.java ファイルの表示

@@ -19,37 +19,67 @@ 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> e: entries) {
24
+            count += e.size();
25
+        }
26
+        return count;
23 27
     }
24 28
 
25 29
     @Override
26 30
     public boolean isEmptry() {
27
-        return false;
31
+        return this.size() == 0;
28 32
     }
29 33
 
30 34
     @Override
31 35
     public Cart get(User key) {
36
+        int bucket = bucketIndex(key);
37
+        for (Entry e: entries[bucket]) {
38
+            if (e.getKey().equals(key)) {
39
+                return e.getValue();
40
+            }
41
+        }
32 42
         return null;
33 43
     }
34 44
 
35 45
     @Override
36 46
     public void put(User key, Cart value) {
37
-
47
+        entries[bucketIndex(key)].add(new Entry(key, value));
38 48
     }
39 49
 
40 50
     @Override
41 51
     public List<User> getKeys() {
42
-        return null;
52
+        List<User> keys = new ArrayList<>();
53
+        for (List<Entry> list:entries) {
54
+            for (Entry e: list )
55
+                keys.add(e.getKey());
56
+        }
57
+        return keys;
43 58
     }
44 59
 
45 60
     @Override
46 61
     public List<Cart> getValues() {
47
-        return null;
62
+        List<Cart> values = new ArrayList<>();
63
+        for (List<Entry> list:entries) {
64
+            for (Entry e: list )
65
+                values.add(e.getValue());
66
+        }
67
+        return values;
48 68
     }
49 69
 
50 70
     @Override
51 71
     public Cart remove(User key) {
52
-        return null;
72
+        int bucket = bucketIndex(key);
73
+        Entry e = new Entry(null, null);
74
+        Iterator<Entry> iterator = entries[bucket].iterator();
75
+        while (iterator.hasNext()) {
76
+            e = iterator.next();
77
+            if (e.getKey().equals(key)) {
78
+                entries[bucket].remove(e);
79
+            }
80
+        }
81
+
82
+        return e.getValue();
53 83
     }
54 84
 
55 85
     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
 }