Преглед изворни кода

Finshed and I understand hashMapping now

Nicholas Maidanos пре 6 година
родитељ
комит
4c5bb4b360

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

+ 50
- 9
src/main/java/org/zipcoder/store/ListMap.java Прегледај датотеку

@@ -9,40 +9,81 @@ public class ListMap implements MyMap {
9 9
 
10 10
     @Override
11 11
     public int size() {
12
-        return -1;
12
+        return this.entries.size();
13 13
     }
14 14
 
15 15
     @Override
16
-    public boolean isEmptry() {
17
-        return false;
16
+    public boolean isEmpty() {
17
+        return this.entries.isEmpty();
18 18
     }
19 19
 
20 20
     @Override
21 21
     public Cart get(User key) {
22
-        // find the cart associate with the key
23
-        return null;
22
+        Cart cart = null;
23
+        for (Entry entry: entries) {
24
+            if(key.equals(entry.getKey())){
25
+                cart = entry.getValue();
26
+                break;
27
+            }
28
+        }
29
+        return cart;
30
+    }
31
+
32
+    public Integer getId(User key) {
33
+    Integer index = null;
34
+        for(int i =0; i < this.entries.size(); i++){
35
+            if(this.entries.get(i).getKey().equals(key)){
36
+                index = i;
37
+                break;
38
+            }
39
+        }
40
+        return index;
24 41
     }
25 42
 
26 43
     @Override
27 44
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
45
+        // add the key, value entry to the entries list'
46
+       Integer index = this.getId(key);
47
+        if(index != null){
48
+            this.entries.get(index).setValue(value);
49
+        } else {
50
+            Entry entry = new Entry(key, value);
51
+            this.entries.add(entry);
52
+        }
29 53
     }
30 54
 
31 55
 
32 56
     @Override
33 57
     public List<User> getKeys() {
34 58
         // get all the keys in the map
35
-        return null;
59
+        List<User> users =  new ArrayList<>();
60
+        for (Entry entry : this.entries) {
61
+            users.add(entry.getKey());
62
+        }
63
+        return users;
36 64
     }
37 65
 
38 66
     @Override
39 67
     public List<Cart> getValues() {
40 68
         // get all the values in the map
41
-        return null;
69
+        List<Cart> values =  new ArrayList<>();
70
+        for (Entry entry : this.entries) {
71
+            values.add(entry.getValue());
72
+        }
73
+        return values;
42 74
     }
43 75
 
44 76
     @Override
45 77
     public Cart remove(User key) {
46
-        return null;
78
+        Cart cart = null;
79
+        for (int i = 0; i < this.entries.size(); i++) {
80
+            if (this.entries.get(i).getKey().equals(key)) {
81
+                cart = this.entries.get(i).getValue();
82
+                this.entries.remove(i);
83
+                break;
84
+            }
85
+        }
86
+        return cart;
47 87
     }
88
+
48 89
 }

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

@@ -1,10 +1,12 @@
1 1
 package org.zipcoder.store;
2 2
 
3
+import sun.jvm.hotspot.utilities.Hashtable;
4
+
3 5
 import java.util.*;
4 6
 
5 7
 public class MyHashMap implements MyMap{
6 8
     // final field that doesn't change for any object
7
-    private static final int BUCKET_SIZE = 15;
9
+    private static final int BUCKET_SIZE = 150;
8 10
 
9 11
     // instance field that can only be set once. It is different for every Object
10 12
     private final List<Entry>[] entries;
@@ -19,37 +21,87 @@ public class MyHashMap implements MyMap{
19 21
 
20 22
     @Override
21 23
     public int size() {
22
-        return -1;
24
+        int total = 0;
25
+        for (List<Entry> entries :this.entries) {
26
+            total += entries.size();
27
+        }
28
+        return total;
23 29
     }
24 30
 
25 31
     @Override
26
-    public boolean isEmptry() {
27
-        return false;
32
+    public boolean isEmpty() {
33
+        return this.size() == 0 ? true : false;
28 34
     }
29 35
 
30 36
     @Override
31 37
     public Cart get(User key) {
32
-        return null;
38
+        Cart cart = null;
39
+        int index = bucketIndex(key);
40
+        for (Entry entry :this.entries[index]) {
41
+            if(entry.getKey().equals(key)){
42
+                cart = entry.getValue();
43
+                break;
44
+            }
45
+        }
46
+        return cart;
33 47
     }
34 48
 
35 49
     @Override
36 50
     public void put(User key, Cart value) {
51
+        int index = bucketIndex(key);
52
+        boolean isPresent = false;
53
+        for (Entry entry1: this.entries[bucketIndex(key)]) {
54
+            if(entry1.getKey().equals(key)){
55
+                entry1.setValue(value);
56
+                isPresent = true;
57
+                break;
58
+            }
59
+        }
60
+
61
+        if(isPresent == false){
62
+            Entry entry = new Entry(key, value);
63
+            this.entries[index].add(entry);
64
+        }
65
+
66
+
37 67
 
38 68
     }
39 69
 
40 70
     @Override
41 71
     public List<User> getKeys() {
42
-        return null;
72
+        ArrayList<User> users = new ArrayList<>();
73
+        for (List<Entry> entries: this.entries) {
74
+            for(Entry entry : entries){
75
+                users.add(entry.getKey());
76
+            }
77
+        }
78
+        return users;
43 79
     }
44 80
 
45 81
     @Override
46 82
     public List<Cart> getValues() {
47
-        return null;
83
+        ArrayList<Cart> carts = new ArrayList<>();
84
+        for (List<Entry> entries: this.entries) {
85
+            for(Entry entry : entries) {
86
+                carts.add(entry.getValue());
87
+            }
88
+        }
89
+        return carts;
48 90
     }
49 91
 
50 92
     @Override
51 93
     public Cart remove(User key) {
52
-        return null;
94
+        Cart cart = null;
95
+        for (List<Entry> entries: this.entries) {
96
+            for (Entry entry: entries) {
97
+                if (entry.getKey().equals(key)){
98
+                    cart = entry.getValue();
99
+                    entries.remove(entry);
100
+                    break;
101
+                }
102
+            }
103
+        }
104
+        return cart;
53 105
     }
54 106
 
55 107
     private int bucketIndex(User key){

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

@@ -12,7 +12,7 @@ public interface MyMap {
12 12
     /**
13 13
      * @return true if the map has one or more item in it. False otherwise.
14 14
      */
15
-    public boolean isEmptry();
15
+    public boolean isEmpty();
16 16
 
17 17
     /**
18 18
      * Return the cart associated with the user

+ 15
- 0
src/main/java/org/zipcoder/store/User.java Прегледај датотеку

@@ -37,4 +37,19 @@ public class User {
37 37
     public void setName(String name) {
38 38
         this.name = name;
39 39
     }
40
+
41
+    @Override
42
+    public boolean equals(Object o) {
43
+        if (this == o) return true;
44
+        if (o == null || getClass() != o.getClass()) return false;
45
+        User user = (User) o;
46
+        return id == user.id &&
47
+                Objects.equals(name, user.name);
48
+    }
49
+
50
+    @Override
51
+    public int hashCode() {
52
+
53
+        return Objects.hash(id, name);
54
+    }
40 55
 }

+ 53
- 53
src/test/java/org/zipcoder/store/ListMapTest.java Прегледај датотеку

@@ -116,7 +116,7 @@ public class ListMapTest {
116 116
 
117 117
     @Test
118 118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120 120
     }
121 121
 
122 122
     @Test
@@ -129,7 +129,7 @@ public class ListMapTest {
129 129
         myMap.put(user1, cart1);
130 130
 
131 131
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
132
+        Assert.assertFalse(myMap.isEmpty());
133 133
     }
134 134
 
135 135
     @Test
@@ -175,57 +175,57 @@ public class ListMapTest {
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
-//    }
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 197
 
198 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
-//    }
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
+    }
231 231
 }

+ 56
- 54
src/test/java/org/zipcoder/store/MyHashMapTest.java Прегледај датотеку

@@ -116,7 +116,7 @@ public class MyHashMapTest {
116 116
 
117 117
     @Test
118 118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120 120
     }
121 121
 
122 122
     @Test
@@ -128,8 +128,10 @@ public class MyHashMapTest {
128 128
         // When
129 129
         myMap.put(user1, cart1);
130 130
 
131
+        System.out.println(myMap.size());
132
+
131 133
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
134
+        Assert.assertFalse(myMap.isEmpty());
133 135
     }
134 136
 
135 137
     @Test
@@ -175,56 +177,56 @@ public class MyHashMapTest {
175 177
     }
176 178
 
177 179
 //    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
-//    }
180
+    @Test
181
+    public void testPutAndGet_WithManyItems(){
182
+        // Given
183
+        int numberOfEntries = 70000;
184
+
185
+        for (int i = 0; i < numberOfEntries; i++) {
186
+            myMap.put(new User(i), new Cart(i));
187
+        }
188
+
189
+
190
+        for (int i = 0; i < numberOfEntries; i++) {
191
+            // When
192
+            Cart actualCart = myMap.get(new User(i));
193
+            Cart expectedCart = new Cart(i);
194
+
195
+            // Then
196
+            Assert.assertEquals(actualCart, expectedCart);
197
+        }
198
+    }
199
+
200
+    @Test
201
+    public void testRemove(){
202
+        // Given
203
+        User user1 = new User("Lena");
204
+        Cart cart1 = new Cart(11);
205
+
206
+        User user2 = new User("Jamal");
207
+        Cart cart2 = new Cart(21);
208
+
209
+        User user3 = new User("Kelvin");
210
+        Cart cart3 = new Cart(31);
211
+
212
+        // When
213
+        myMap.put(user1, cart1);
214
+        myMap.put(user2, cart2);
215
+        myMap.put(user3, cart3);
216
+
217
+        // Then
218
+        Cart actualCart = myMap.remove(user2);
219
+
220
+        Assert.assertEquals(cart2, actualCart);
221
+        Assert.assertNull(myMap.get(user2));
222
+    }
223
+
224
+    @Test
225
+    public void testRemove_CartNotInTheMap(){
226
+        // Given
227
+        User user1 = new User("Lena");
228
+
229
+        // When & Then
230
+        Assert.assertNull(myMap.remove(user1));
231
+    }
230 232
 }