Joshua Chung 6 lat temu
rodzic
commit
e5c39f8b21

+ 49
- 8
src/main/java/org/zipcoder/store/MyHashMap.java Wyświetl plik

@@ -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,81 @@ 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
+
32
+        if (size() == 0) return true;
33
+        else return false;
28 34
     }
29 35
 
30 36
     @Override
31 37
     public Cart get(User key) {
32
-        return null;
38
+        List<Entry> bucket = entries[bucketIndex(key)];
39
+        int index = keyIndex(bucket, key);
40
+        if (index >= 0 ) return bucket.get(keyIndex(bucket,key)).getValue();
41
+        else return null;
33 42
     }
34 43
 
35 44
     @Override
36 45
     public void put(User key, Cart value) {
37
-
46
+        entries[bucketIndex(key)].add(new Entry(key, value));
38 47
     }
39 48
 
40 49
     @Override
41 50
     public List<User> getKeys() {
42
-        return null;
51
+        List<User> keys = new ArrayList<User>();
52
+        for (List<Entry> list : entries) {
53
+            for (Entry entry : list) {
54
+                keys.add(entry.getKey());
55
+            }
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<Cart>();
63
+        for(List<Entry> list : entries) {
64
+            for (Entry e : list) {
65
+                values.add(e.getValue());
66
+            }
67
+        }
68
+        return values;
48 69
     }
49 70
 
50 71
     @Override
51 72
     public Cart remove(User key) {
52
-        return null;
73
+        List<Entry> bucket = entries[bucketIndex(key)];
74
+        Iterator<Entry> iterator = bucket.iterator();
75
+        Cart removed = null;
76
+        while(iterator.hasNext()) {
77
+            Entry entry = iterator.next();
78
+            if(entry.getKey().equals(key)) {
79
+                removed = entry.getValue();
80
+                iterator.remove();
81
+            }
82
+        }
83
+        return removed;
53 84
     }
54 85
 
55 86
     private int bucketIndex(User key){
56 87
         return Math.abs(key.hashCode()) % entries.length;
57 88
     }
89
+
90
+    private int keyIndex(List<Entry> bucket, User key) {
91
+        int index = -1;
92
+        for (int i = 0; i < bucket.size(); i++) {
93
+            if (bucket.get(i).getKey().equals(key)){
94
+                index = i;
95
+            }
96
+        }
97
+        return index;
98
+    }
58 99
 }

+ 3
- 0
src/main/java/org/zipcoder/store/User.java Wyświetl plik

@@ -10,10 +10,13 @@ public class User {
10 10
     public User(){}
11 11
 
12 12
     public User(long id) {
13
+
13 14
         this.id = id;
15
+        name = "";
14 16
     }
15 17
 
16 18
     public User(String name) {
19
+        id = 0;
17 20
         this.name = name;
18 21
     }
19 22
 

+ 227
- 227
src/test/java/org/zipcoder/store/MyHashMapTest.java Wyświetl plik

@@ -1,230 +1,230 @@
1
-//package org.zipcoder.store;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Before;
5
-//import org.junit.Test;
6
-//
7
-//import java.util.List;
8
-//
9
-//public class MyHashMapTest {
10
-//    private MyMap myMap;
11
-//
12
-//    @Before
13
-//    public void setup(){
14
-//        myMap = new MyHashMap();
15
-//    }
16
-//
17
-//    @Test
18
-//    public void testPutAndGet_WithOneItem(){
19
-//        User user = new User("Tiana");
20
-//        Cart cart = new Cart(1);
21
-//
22
-//        // When
23
-//        myMap.put(user, cart);
24
-//
25
-//        // Then
26
-//        Cart actualCart = myMap.get(user);
27
-//        Assert.assertEquals(cart, actualCart);
28
-//    }
29
-//
30
-//    @Test
31
-//    public void testPutAndGet_WithTwoItems(){
32
-//        User tiana = new User("Tiana");
33
-//        Cart tianasCart = new Cart(1134);
34
-//
35
-//        User john = new User("John");
36
-//        Cart johnsCart = new Cart(42);
37
-//
38
-//        // When
39
-//        myMap.put(john, johnsCart);
40
-//        myMap.put(tiana, tianasCart);
41
-//
42
-//        // Then
43
-//        Cart actualJohnsCart = myMap.get(john);
44
-//        Assert.assertEquals(johnsCart, actualJohnsCart);
45
-//
46
-//        Cart actualTianasCart = myMap.get(tiana);
47
-//        Assert.assertEquals(tianasCart, actualTianasCart);
48
-//    }
49
-//
50
-//    @Test
51
-//    public void testPutAndGet_UpdateCart(){
52
-//        User tiana = new User("Tiana");
53
-//        Cart tianasCart = new Cart(11);
54
-//        myMap.put(tiana, tianasCart);
55
-//
56
-//        // When
57
-//        Cart updatedCart = new Cart(99);
58
-//        myMap.put(tiana, updatedCart);
59
-//
60
-//        // Then
61
-//        Cart actualCart = myMap.get(tiana);
62
-//        Assert.assertEquals(updatedCart, actualCart);
63
-//    }
64
-//
65
-//    @Test
66
-//    public void testGetWithoutPut(){
67
-//        // Given
68
-//        User user = new User("Tiana");
69
-//
70
-//        // When
71
-//        Cart actualCart = myMap.get(user);
72
-//
73
-//        // Then
74
-//        Assert.assertNull(actualCart);
75
-//    }
76
-//
77
-//    @Test
78
-//    public void testPutAndGet_WithDifferentKeyInstance(){
79
-//        // Given
80
-//        String name = "Tiana";
81
-//        User tiana = new User(name);
82
-//        Cart cart = new Cart(76);
83
-//
84
-//        // When
85
-//        myMap.put(tiana, cart);
86
-//        User cloneTiana = new User(name);
87
-//        Cart actualCart = myMap.get(cloneTiana);
88
-//
89
-//        // Then
90
-//        Assert.assertEquals(cart, actualCart);
91
-//    }
92
-//
93
-//    @Test
94
-//    public void testSize_WithNoEntry(){
95
-//        int expectedSize = 0;
96
-//        Assert.assertEquals(expectedSize, myMap.size());
97
-//    }
98
-//
99
-//    @Test
100
-//    public void testSize_WithEntries(){
101
-//        // Given
102
-//        User user1 = new User("Monica");
103
-//        Cart cart1 = new Cart(45);
104
-//
105
-//        User user2 = new User("Quincy");
106
-//        Cart cart2 = new Cart(78);
107
-//
108
-//        // When
109
-//        myMap.put(user1, cart1);
110
-//        myMap.put(user2, cart2);
111
-//
112
-//        // Then
113
-//        int expectedSize = 2;
114
-//        Assert.assertEquals(expectedSize, myMap.size());
115
-//    }
116
-//
117
-//    @Test
118
-//    public void testIsEmpty_WithoutEntry(){
119
-//        Assert.assertTrue(myMap.isEmptry());
120
-//    }
121
-//
122
-//    @Test
123
-//    public void testisEmpty_WithEntries(){
124
-//        // Given
125
-//        User user1 = new User("Monica");
126
-//        Cart cart1 = new Cart(45);
127
-//
128
-//        // When
129
-//        myMap.put(user1, cart1);
130
-//
131
-//        // Then
132
-//        Assert.assertFalse(myMap.isEmptry());
133
-//    }
134
-//
135
-//    @Test
136
-//    public void testGetKeys(){
137
-//        // Given
138
-//        User user1 = new User("Zeke");
139
-//        Cart cart1 = new Cart(7);
140
-//
141
-//        User user2 = new User("Nona");
142
-//        Cart cart2 = new Cart(29);
143
-//
144
-//        // When
145
-//        myMap.put(user1, cart1);
146
-//        myMap.put(user2, cart2);
147
-//
148
-//        // Then
149
-//        List<User> keys = myMap.getKeys();
150
-//        int expectedKeySize = 2;
151
-//        Assert.assertEquals(expectedKeySize, keys.size());
152
-//        Assert.assertTrue(keys.contains(user1));
153
-//        Assert.assertTrue(keys.contains(user2));
154
-//    }
155
-//
156
-//    @Test
157
-//    public void testGetValues(){
158
-//        // Given
159
-//        User user1 = new User("Camille");
160
-//        Cart cart1 = new Cart(433);
161
-//
162
-//        User user2 = new User("Nathan");
163
-//        Cart cart2 = new Cart(71);
164
-//
165
-//        // When
166
-//        myMap.put(user1, cart1);
167
-//        myMap.put(user2, cart2);
168
-//
169
-//        // Then
170
-//        List<Cart> values = myMap.getValues();
171
-//        int expectedKeySize = 2;
172
-//        Assert.assertEquals(expectedKeySize, values.size());
173
-//        Assert.assertTrue(values.contains(cart1));
174
-//        Assert.assertTrue(values.contains(cart2));
175
-//    }
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.List;
8
+
9
+public class MyHashMapTest {
10
+    private MyMap myMap;
11
+
12
+    @Before
13
+    public void setup(){
14
+        myMap = new MyHashMap();
15
+    }
16
+
17
+    @Test
18
+    public void testPutAndGet_WithOneItem(){
19
+        User user = new User("Tiana");
20
+        Cart cart = new Cart(1);
21
+
22
+        // When
23
+        myMap.put(user, cart);
24
+
25
+        // Then
26
+        Cart actualCart = myMap.get(user);
27
+        Assert.assertEquals(cart, actualCart);
28
+    }
29
+
30
+    @Test
31
+    public void testPutAndGet_WithTwoItems(){
32
+        User tiana = new User("Tiana");
33
+        Cart tianasCart = new Cart(1134);
34
+
35
+        User john = new User("John");
36
+        Cart johnsCart = new Cart(42);
37
+
38
+        // When
39
+        myMap.put(john, johnsCart);
40
+        myMap.put(tiana, tianasCart);
41
+
42
+        // Then
43
+        Cart actualJohnsCart = myMap.get(john);
44
+        Assert.assertEquals(johnsCart, actualJohnsCart);
45
+
46
+        Cart actualTianasCart = myMap.get(tiana);
47
+        Assert.assertEquals(tianasCart, actualTianasCart);
48
+    }
49
+
50
+    @Test
51
+    public void testPutAndGet_UpdateCart(){
52
+        User tiana = new User("Tiana");
53
+        Cart tianasCart = new Cart(11);
54
+        myMap.put(tiana, tianasCart);
55
+
56
+        // When
57
+        Cart updatedCart = new Cart(99);
58
+        myMap.put(tiana, updatedCart);
59
+
60
+        // Then
61
+        Cart actualCart = myMap.get(tiana);
62
+        Assert.assertEquals(updatedCart, actualCart);
63
+    }
64
+
65
+    @Test
66
+    public void testGetWithoutPut(){
67
+        // Given
68
+        User user = new User("Tiana");
69
+
70
+        // When
71
+        Cart actualCart = myMap.get(user);
72
+
73
+        // Then
74
+        Assert.assertNull(actualCart);
75
+    }
76
+
77
+    @Test
78
+    public void testPutAndGet_WithDifferentKeyInstance(){
79
+        // Given
80
+        String name = "Tiana";
81
+        User tiana = new User(name);
82
+        Cart cart = new Cart(76);
83
+
84
+        // When
85
+        myMap.put(tiana, cart);
86
+        User cloneTiana = new User(name);
87
+        Cart actualCart = myMap.get(cloneTiana);
88
+
89
+        // Then
90
+        Assert.assertEquals(cart, actualCart);
91
+    }
92
+
93
+    @Test
94
+    public void testSize_WithNoEntry(){
95
+        int expectedSize = 0;
96
+        Assert.assertEquals(expectedSize, myMap.size());
97
+    }
98
+
99
+    @Test
100
+    public void testSize_WithEntries(){
101
+        // Given
102
+        User user1 = new User("Monica");
103
+        Cart cart1 = new Cart(45);
104
+
105
+        User user2 = new User("Quincy");
106
+        Cart cart2 = new Cart(78);
107
+
108
+        // When
109
+        myMap.put(user1, cart1);
110
+        myMap.put(user2, cart2);
111
+
112
+        // Then
113
+        int expectedSize = 2;
114
+        Assert.assertEquals(expectedSize, myMap.size());
115
+    }
116
+
117
+    @Test
118
+    public void testIsEmpty_WithoutEntry(){
119
+        Assert.assertTrue(myMap.isEmptry());
120
+    }
121
+
122
+    @Test
123
+    public void testisEmpty_WithEntries(){
124
+        // Given
125
+        User user1 = new User("Monica");
126
+        Cart cart1 = new Cart(45);
127
+
128
+        // When
129
+        myMap.put(user1, cart1);
130
+
131
+        // Then
132
+        Assert.assertFalse(myMap.isEmptry());
133
+    }
134
+
135
+    @Test
136
+    public void testGetKeys(){
137
+        // Given
138
+        User user1 = new User("Zeke");
139
+        Cart cart1 = new Cart(7);
140
+
141
+        User user2 = new User("Nona");
142
+        Cart cart2 = new Cart(29);
143
+
144
+        // When
145
+        myMap.put(user1, cart1);
146
+        myMap.put(user2, cart2);
147
+
148
+        // Then
149
+        List<User> keys = myMap.getKeys();
150
+        int expectedKeySize = 2;
151
+        Assert.assertEquals(expectedKeySize, keys.size());
152
+        Assert.assertTrue(keys.contains(user1));
153
+        Assert.assertTrue(keys.contains(user2));
154
+    }
155
+
156
+    @Test
157
+    public void testGetValues(){
158
+        // Given
159
+        User user1 = new User("Camille");
160
+        Cart cart1 = new Cart(433);
161
+
162
+        User user2 = new User("Nathan");
163
+        Cart cart2 = new Cart(71);
164
+
165
+        // When
166
+        myMap.put(user1, cart1);
167
+        myMap.put(user2, cart2);
168
+
169
+        // Then
170
+        List<Cart> values = myMap.getValues();
171
+        int expectedKeySize = 2;
172
+        Assert.assertEquals(expectedKeySize, values.size());
173
+        Assert.assertTrue(values.contains(cart1));
174
+        Assert.assertTrue(values.contains(cart2));
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
 }