diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..a874d27 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -4,7 +4,6 @@ import java.util.ArrayList; import java.util.HashMap; - public class Exercise extends ExerciseBase { /* The final fundamental building block of Java is a Map. There is still much to learn about the language, @@ -48,6 +47,12 @@ public HashMap createPerson() { in the createPerson method */ + public String getValue(String stringValue) { + return createPerson().get(stringValue); + } + + + /* @@ -58,8 +63,10 @@ public HashMap createPerson() { in the provided HashMap */ + public boolean hasKey(HashMap map, String key) { + return map.containsKey(key); - + } /* TODO: 3. Create a method named getValueOrDefault that accepts two parameters: - A HashMap of String, Integer key value pairs @@ -68,6 +75,10 @@ public HashMap createPerson() { or -1 if the string provided is not a key in the HashMap */ + public int getValueOrDefault(HashMap map, String key) { + return map.getOrDefault(key, -1); + } + /* @@ -88,14 +99,19 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(6712, "bass"); map.put(7, "muse"); map.put(96, "nice"); - // Write your code below this comment... - + // Write your code elow this comment... + ArrayList secretPhrase = new ArrayList<>(); + for (Integer number : numbers) { + if (map.containsKey(number)) { + secretPhrase.add(map.get(number)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return secretPhrase; } }