Bläddra i källkod

completed lab

Allison Ziegler 6 år sedan
förälder
incheckning
f16c5ad803

+ 47
- 8
src/main/java/org/zipcoder/store/MyHashMap.java Visa fil

4
 
4
 
5
 public class MyHashMap implements MyMap{
5
 public class MyHashMap implements MyMap{
6
     // final field that doesn't change for any object
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
     // instance field that can only be set once. It is different for every Object
9
     // instance field that can only be set once. It is different for every Object
10
     private final List<Entry>[] entries;
10
     private final List<Entry>[] entries;
19
 
19
 
20
     @Override
20
     @Override
21
     public int size() {
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
     @Override
29
     @Override
26
     public boolean isEmptry() {
30
     public boolean isEmptry() {
27
-        return false;
31
+        if (size() == 0) return true;
32
+        else return false;
28
     }
33
     }
29
 
34
 
30
     @Override
35
     @Override
31
     public Cart get(User key) {
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
     @Override
43
     @Override
36
     public void put(User key, Cart value) {
44
     public void put(User key, Cart value) {
37
-
45
+        entries[bucketIndex(key)].add(new Entry(key, value));
38
     }
46
     }
39
 
47
 
40
     @Override
48
     @Override
41
     public List<User> getKeys() {
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
     @Override
59
     @Override
46
     public List<Cart> getValues() {
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
     @Override
70
     @Override
51
     public Cart remove(User key) {
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
     private int bucketIndex(User key){
85
     private int bucketIndex(User key){
56
         return Math.abs(key.hashCode()) % entries.length;
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 Visa fil

175
     }
175
     }
176
 
176
 
177
 //    I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
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
 }