|
@@ -8,10 +8,12 @@ public class MyHashMap implements MyMap{
|
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
|
+ private int size;
|
11
|
12
|
|
12
|
13
|
public MyHashMap(){
|
|
14
|
+ // creates an array of length 15
|
13
|
15
|
entries = new List[BUCKET_SIZE];
|
14
|
|
-
|
|
16
|
+ // populates array with 15 ArrayLists
|
15
|
17
|
for(int i = 0; i < BUCKET_SIZE; i++) {
|
16
|
18
|
entries[i] = new ArrayList<>();
|
17
|
19
|
}
|
|
@@ -19,37 +21,66 @@ public class MyHashMap implements MyMap{
|
19
|
21
|
|
20
|
22
|
@Override
|
21
|
23
|
public int size() {
|
22
|
|
- return -1;
|
|
24
|
+ for (int i = 0; i < entries.length; i++) {
|
|
25
|
+ size += entries[i].size();
|
|
26
|
+ }
|
|
27
|
+ return size;
|
23
|
28
|
}
|
24
|
29
|
|
25
|
30
|
@Override
|
26
|
31
|
public boolean isEmptry() {
|
27
|
|
- return false;
|
|
32
|
+ return size == 0;
|
28
|
33
|
}
|
29
|
34
|
|
30
|
35
|
@Override
|
31
|
36
|
public Cart get(User key) {
|
32
|
|
- return null;
|
|
37
|
+ Cart rightCart = null;
|
|
38
|
+ for (List<Entry> e : entries) {
|
|
39
|
+ for (Entry entry : e) {
|
|
40
|
+ if(entry.getKey().equals(key) || entry.getKey().getName().equals(key.getName())) {
|
|
41
|
+ rightCart = entry.getValue();
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ return rightCart;
|
33
|
46
|
}
|
34
|
47
|
|
35
|
48
|
@Override
|
36
|
49
|
public void put(User key, Cart value) {
|
37
|
|
-
|
|
50
|
+ Entry entry = new Entry(key, value);
|
|
51
|
+ entries[this.size++].add(entry);
|
|
52
|
+ //this.size++;
|
38
|
53
|
}
|
39
|
54
|
|
40
|
55
|
@Override
|
41
|
56
|
public List<User> getKeys() {
|
42
|
|
- return null;
|
|
57
|
+ List<User> keyList = new ArrayList<>();
|
|
58
|
+ List<List<Entry>> lambdaList = new ArrayList<>(Arrays.asList(entries));
|
|
59
|
+
|
|
60
|
+ lambdaList.forEach(entries1 -> entries1.forEach(entry -> keyList.add(entry.getKey())));
|
|
61
|
+ return keyList;
|
43
|
62
|
}
|
44
|
63
|
|
45
|
64
|
@Override
|
46
|
65
|
public List<Cart> getValues() {
|
47
|
|
- return null;
|
|
66
|
+ List<Cart> valueList = new ArrayList<>();
|
|
67
|
+ List<List<Entry>> lambdaList = new ArrayList<>(Arrays.asList(entries));
|
|
68
|
+
|
|
69
|
+ lambdaList.forEach(entries1 -> entries1.forEach(entry -> valueList.add(entry.getValue())));
|
|
70
|
+ return valueList;
|
48
|
71
|
}
|
49
|
72
|
|
50
|
73
|
@Override
|
51
|
74
|
public Cart remove(User key) {
|
52
|
|
- return null;
|
|
75
|
+ Cart rightCart = null;
|
|
76
|
+ for (List<Entry> list : entries) {
|
|
77
|
+ for (Entry entry : list) {
|
|
78
|
+ if (entry.getKey() == key) {
|
|
79
|
+ rightCart = entry.getValue();
|
|
80
|
+ }
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ return rightCart;
|
53
|
84
|
}
|
54
|
85
|
|
55
|
86
|
private int bucketIndex(User key){
|