Skip to content

Commit 24f47f7

Browse files
committed
Solution
1 parent 5510af5 commit 24f47f7

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/main/java/com/booleanuk/core/Exercise.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.HashMap;
7+
import java.util.HashSet;
78

89
public class Exercise extends ExerciseBase {
910
/*
@@ -47,7 +48,9 @@ public HashMap<String, String> createPerson() {
4748
The method must return the value associated to the provided key from the HashMap created
4849
in the createPerson method
4950
*/
50-
51+
public String getValue(String getVal){
52+
return createPerson().get(getVal);
53+
}
5154

5255

5356
/*
@@ -57,7 +60,9 @@ public HashMap<String, String> createPerson() {
5760
The method must return a boolean that represents whether the string provided exists as a key
5861
in the provided HashMap
5962
*/
60-
63+
public boolean hasKey(HashMap<String, String> map, String key){
64+
return map.containsKey(key);
65+
}
6166

6267

6368
/*
@@ -67,7 +72,16 @@ public HashMap<String, String> createPerson() {
6772
The method must use the string provided to return the integer contained in the provided HashMap,
6873
or -1 if the string provided is not a key in the HashMap
6974
*/
75+
public int getValueOrDefault(HashMap<String, Integer> map, String str){
76+
Object ret;
77+
ret = map.get(str);
78+
if (ret == null) {
79+
return -1;
80+
}
81+
82+
return (Integer) ret;
7083

84+
}
7185

7286

7387
/*
@@ -90,12 +104,18 @@ public ArrayList<String> buildSecretPhrase(ArrayList<Integer> numbers) {
90104
map.put(96, "nice");
91105
// Write your code below this comment...
92106

107+
ArrayList<String> strs = new ArrayList<>();
93108

109+
for (int curr: numbers){
110+
if (map.get(curr) != null){
111+
strs.add(map.get(curr));
112+
}
113+
}
94114

95115

96116
// ...and above this comment
97117

98118
// Change the return statement below to return your actual ArrayList
99-
return new ArrayList<String>();
119+
return strs;
100120
}
101121
}

0 commit comments

Comments
 (0)