From 24f47f7e21c5238c01c680686f9cd54bfd6cebbe Mon Sep 17 00:00:00 2001 From: Lucas Holter Date: Wed, 6 Aug 2025 13:34:15 +0200 Subject: [PATCH] Solution --- .../java/com/booleanuk/core/Exercise.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..d331916 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; public class Exercise extends ExerciseBase { /* @@ -47,7 +48,9 @@ public HashMap createPerson() { The method must return the value associated to the provided key from the HashMap created in the createPerson method */ - + public String getValue(String getVal){ + return createPerson().get(getVal); + } /* @@ -57,7 +60,9 @@ public HashMap createPerson() { The method must return a boolean that represents whether the string provided exists as a key in the provided HashMap */ - + public boolean hasKey(HashMap map, String key){ + return map.containsKey(key); + } /* @@ -67,7 +72,16 @@ public HashMap createPerson() { The method must use the string provided to return the integer contained in the provided HashMap, or -1 if the string provided is not a key in the HashMap */ + public int getValueOrDefault(HashMap map, String str){ + Object ret; + ret = map.get(str); + if (ret == null) { + return -1; + } + + return (Integer) ret; + } /* @@ -90,12 +104,18 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... + ArrayList strs = new ArrayList<>(); + for (int curr: numbers){ + if (map.get(curr) != null){ + strs.add(map.get(curr)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return strs; } }