From 122aa941b110a3747df812edaa9baae6f4e4a027 Mon Sep 17 00:00:00 2001 From: Frida Anselin Date: Wed, 8 Jan 2025 14:48:52 +0100 Subject: [PATCH] Completed fundamentals maps core exercises --- .../java/com/booleanuk/core/Exercise.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..572fb07 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -47,7 +47,10 @@ 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 key) { + HashMap person = createPerson(); + return person.get(key); + } /* @@ -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 person, String attribute) { + return person.containsKey(attribute); + } /* @@ -68,7 +73,9 @@ public HashMap createPerson() { or -1 if the string provided is not a key in the HashMap */ - + public int getValueOrDefault(HashMap person, String attribute) { + return person.getOrDefault(attribute,-1); + } /* TODO: 4. Complete the method below @@ -90,12 +97,17 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... - - + ArrayList secret = new ArrayList<>(); + for (int number : numbers) { + if (map.containsKey(number)) { + String word = map.get(number); + secret.add(word); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return secret; } }