From 1120855f55923313cebd93cbf8aeaab992cfff4b Mon Sep 17 00:00:00 2001 From: Mattias Hedbom Date: Wed, 8 Jan 2025 12:59:17 +0100 Subject: [PATCH] Maps completed --- .../java/com/booleanuk/core/Exercise.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..efcdb9e 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -47,6 +47,11 @@ 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 map = createPerson(); + return map.get(key); + } + @@ -57,6 +62,11 @@ 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,6 +77,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 key){ + if(map.containsKey(key)){ + return map.get(key); + } + else{ + return -1; + } + } + + @@ -90,12 +110,18 @@ public ArrayList buildSecretPhrase(ArrayList numbers) { map.put(96, "nice"); // Write your code below this comment... + ArrayList values = new ArrayList<>(); + for(int number : numbers){ + if(map.containsKey(number)){ + values.add(map.get(number)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return values; } }