diff --git a/src/main/java/com/booleanuk/core/Exercise.java b/src/main/java/com/booleanuk/core/Exercise.java index e1485c3..c2c76b6 100644 --- a/src/main/java/com/booleanuk/core/Exercise.java +++ b/src/main/java/com/booleanuk/core/Exercise.java @@ -47,8 +47,12 @@ 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); + } /* TODO: 2. Create a method named hasKey that accepts two parameters: @@ -58,7 +62,11 @@ public HashMap createPerson() { in the provided HashMap */ + public boolean hasKey(HashMap map ,String random) { + + return map.containsKey(random); + } /* TODO: 3. Create a method named getValueOrDefault that accepts two parameters: @@ -67,6 +75,11 @@ 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) { + + return map.getOrDefault(key, -1); + + } @@ -88,14 +101,20 @@ 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 below this comment... + ArrayList resultList = new ArrayList<>(); + for (Integer number : numbers) { + if (map.containsKey(number)) { + resultList.add(map.get(number)); + } + } // ...and above this comment // Change the return statement below to return your actual ArrayList - return new ArrayList(); + return resultList; } }