소스 검색

completed

Vincent Sima 6 년 전
부모
커밋
9e92f24dc6

+ 1
- 1
README.md 파일 보기

@@ -24,7 +24,7 @@ A list map stores every entry in a list. Every time the user calls `put`, create
24 24
 **BONUS**: Uncomment the remove method and add the code to pass the tests.
25 25
 
26 26
 ### Part 3 - MyHashMap
27
-If you still have no idea what a HashMap is, read this [article](https://medium.com/@nhu313/what-is-a-java-hashmap-83152fb632bb) or watch this [video](https://www.youtube.com/watch?v=shs0KM3wKv8). Notice I add the `bucketIndex` to help you determine where to store the entry. The `bucketIndex` uses the `hashCode` to try to evenly distribute the entries.
27
+If you still have no idea what a HashMap is, read this [article](v) or watch this [video](https://www.youtube.com/watch?v=shs0KM3wKv8). Notice I add the `bucketIndex` to help you determine where to store the entry. The `bucketIndex` uses the `hashCode` to try to evenly distribute the entries.
28 28
   1. `put` - find which bucket it belongs to, then add it to the list of that bucket
29 29
   2. `get` - find which bucket it belongs to, then loop through the list to find the entry corresponding to the key
30 30
   3. Uncomment line 178 - 196. Notice how fast it takes to run.

+ 14
- 0
src/main/java/org/zipcoder/store/Cart.java 파일 보기

@@ -12,4 +12,18 @@ public class Cart {
12 12
     public long getId(){
13 13
         return this.id;
14 14
     }
15
+
16
+    @Override
17
+    public boolean equals(Object o) {
18
+        if (this == o) return true;
19
+        if (o == null || getClass() != o.getClass()) return false;
20
+        Cart cart = (Cart) o;
21
+        return id == cart.id;
22
+    }
23
+
24
+    @Override
25
+    public int hashCode() {
26
+
27
+        return Objects.hash(id);
28
+    }
15 29
 }

+ 35
- 10
src/main/java/org/zipcoder/store/ListMap.java 파일 보기

@@ -4,45 +4,70 @@ import java.util.ArrayList;
4 4
 import java.util.List;
5 5
 
6 6
 public class ListMap implements MyMap {
7
-    // instance field that is intialize to an ArrayList
8 7
     private List<Entry> entries = new ArrayList<>();
9 8
 
10 9
     @Override
11 10
     public int size() {
12
-        return -1;
11
+        return entries.size();
13 12
     }
14 13
 
15 14
     @Override
16 15
     public boolean isEmptry() {
17
-        return false;
16
+        return entries.isEmpty();
18 17
     }
19 18
 
20 19
     @Override
21 20
     public Cart get(User key) {
22
-        // find the cart associate with the key
21
+        for (Entry e : entries) {
22
+            if (e.getKey().equals(key)) {
23
+                return e.getValue();
24
+            }
25
+        }
23 26
         return null;
24 27
     }
25 28
 
26 29
     @Override
27 30
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
31
+        if (get(key) == null) entries.add(new Entry(key, value));
32
+        else {
33
+            for (Entry e : entries) {
34
+                if (e.getKey().equals(key)) {
35
+                    entries.remove(e);
36
+                    e.setValue(value);
37
+                    entries.add(e);
38
+                }
39
+            }
40
+        }
29 41
     }
30 42
 
31 43
 
32 44
     @Override
33 45
     public List<User> getKeys() {
34
-        // get all the keys in the map
35
-        return null;
46
+        List<User> users = new ArrayList<>();
47
+        for (Entry e : entries) {
48
+            users.add(e.getKey());
49
+        }
50
+        return users;
36 51
     }
37 52
 
38 53
     @Override
39 54
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
55
+        List<Cart> carts = new ArrayList<>();
56
+        for (Entry e : entries) {
57
+            carts.add(e.getValue());
58
+        }
59
+        return carts;
42 60
     }
43 61
 
44 62
     @Override
45 63
     public Cart remove(User key) {
46
-        return null;
64
+        Cart cart = null;
65
+        for (Entry e : entries) {
66
+            if (e.getKey().equals(key)) {
67
+                cart = e.getValue();
68
+                entries.remove(e);
69
+            }
70
+        }
71
+        return cart;
47 72
     }
48 73
 }

+ 40
- 9
src/main/java/org/zipcoder/store/MyHashMap.java 파일 보기

@@ -2,49 +2,80 @@ package org.zipcoder.store;
2 2
 
3 3
 import java.util.*;
4 4
 
5
-public class MyHashMap implements MyMap{
5
+public class MyHashMap implements MyMap {
6 6
     // final field that doesn't change for any object
7 7
     private static final int BUCKET_SIZE = 15;
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;
11 11
 
12
-    public MyHashMap(){
12
+    public MyHashMap() {
13 13
         entries = new List[BUCKET_SIZE];
14 14
 
15
-        for(int i = 0; i < BUCKET_SIZE; i++) {
15
+        for (int i = 0; i < BUCKET_SIZE; i++) {
16 16
             entries[i] = new ArrayList<>();
17 17
         }
18 18
     }
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int size = 0;
23
+        for (List<Entry> lst : entries) {
24
+            size += lst.size();
25
+        }
26
+        return size;
23 27
     }
24 28
 
25 29
     @Override
26 30
     public boolean isEmptry() {
27
-        return false;
31
+        if (size() == 0) {
32
+            return true;
33
+        } else {
34
+            return false;
35
+        }
28 36
     }
29 37
 
30 38
     @Override
31 39
     public Cart get(User key) {
32
-        return null;
40
+        Cart c = null;
41
+        List<Entry> entryList = entries[bucketIndex(key)];
42
+        for (Entry e : entryList) {
43
+            if (e.getKey().equals(key)) {
44
+                c = e.getValue();
45
+            }
46
+
47
+        }
48
+        return c;
33 49
     }
34 50
 
35 51
     @Override
36 52
     public void put(User key, Cart value) {
53
+        entries[bucketIndex(key)].add(new Entry(key, value));
37 54
 
38 55
     }
39 56
 
40 57
     @Override
41 58
     public List<User> getKeys() {
42
-        return null;
59
+        List<User> user = new ArrayList<>();
60
+
61
+        for (List<Entry> entryList : entries) {
62
+            for (Entry e : entryList) {
63
+                user.add(e.getKey());
64
+            }
65
+
66
+        }
67
+        return user;
43 68
     }
44 69
 
45 70
     @Override
46 71
     public List<Cart> getValues() {
47
-        return null;
72
+        List<Cart> c = new ArrayList<>();
73
+        for (List<Entry> entryList : entries) {
74
+            for (Entry e : entryList) {
75
+                c.add(e.getValue());
76
+            }
77
+        }
78
+        return c;
48 79
     }
49 80
 
50 81
     @Override
@@ -52,7 +83,7 @@ public class MyHashMap implements MyMap{
52 83
         return null;
53 84
     }
54 85
 
55
-    private int bucketIndex(User key){
86
+    private int bucketIndex(User key) {
56 87
         return Math.abs(key.hashCode()) % entries.length;
57 88
     }
58 89
 }

+ 18
- 2
src/main/java/org/zipcoder/store/User.java 파일 보기

@@ -7,7 +7,8 @@ public class User {
7 7
     private long id;
8 8
     private String name;
9 9
 
10
-    public User(){}
10
+    public User() {
11
+    }
11 12
 
12 13
     public User(long id) {
13 14
         this.id = id;
@@ -17,7 +18,7 @@ public class User {
17 18
         this.name = name;
18 19
     }
19 20
 
20
-    public User(long id, String name){
21
+    public User(long id, String name) {
21 22
         this.id = id;
22 23
         this.name = name;
23 24
     }
@@ -37,4 +38,19 @@ public class User {
37 38
     public void setName(String name) {
38 39
         this.name = name;
39 40
     }
41
+
42
+    public boolean equals(Object obj) {
43
+        boolean answer =false;
44
+                User user = (User) obj;
45
+                if ((this.getId() == user.getId()) && (this.getName() == user.getName())) {
46
+                        answer = true;
47
+                    }
48
+                return answer;
49
+            }
50
+
51
+
52
+    public int hashCode() {
53
+        return Objects.hash(id, name);
54
+
55
+    }
40 56
 }

+ 53
- 54
src/test/java/org/zipcoder/store/ListMapTest.java 파일 보기

@@ -174,58 +174,57 @@ public class ListMapTest {
174 174
         Assert.assertTrue(values.contains(cart2));
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
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
-//    BONUS
199
-//    @Test
200
-//    public void testRemove(){
201
-//        // Given
202
-//        User user1 = new User("Lena");
203
-//        Cart cart1 = new Cart(11);
204
-//
205
-//        User user2 = new User("Jamal");
206
-//        Cart cart2 = new Cart(21);
207
-//
208
-//        User user3 = new User("Kelvin");
209
-//        Cart cart3 = new Cart(31);
210
-//
211
-//        // When
212
-//        myMap.put(user1, cart1);
213
-//        myMap.put(user2, cart2);
214
-//        myMap.put(user3, cart3);
215
-//
216
-//        // Then
217
-//        Cart actualCart = myMap.remove(user2);
218
-//
219
-//        Assert.assertEquals(cart2, actualCart);
220
-//        Assert.assertNull(myMap.get(user2));
221
-//    }
222
-//
223
-//    @Test
224
-//    public void testRemove_CartNotInTheMap(){
225
-//        // Given
226
-//        User user1 = new User("Lena");
227
-//
228
-//        // When & Then
229
-//        Assert.assertNull(myMap.remove(user1));
230
-//    }
177
+    @Test
178
+    public void testPutAndGet_WithManyItems(){
179
+        // Given
180
+        int numberOfEntries = 70000;
181
+
182
+        for (int i = 0; i < numberOfEntries; i++) {
183
+            myMap.put(new User(i), new Cart(i));
184
+        }
185
+
186
+
187
+        for (int i = 0; i < numberOfEntries; i++) {
188
+            // When
189
+            Cart actualCart = myMap.get(new User(i));
190
+            Cart expectedCart = new Cart(i);
191
+
192
+            // Then
193
+            Assert.assertEquals(actualCart, expectedCart);
194
+        }
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
+    }
231 230
 }