44
55import java .util .ArrayList ;
66import java .util .HashMap ;
7+ import java .util .HashSet ;
78
89public class Exercise extends ExerciseBase {
910 /*
@@ -47,7 +48,9 @@ public HashMap<String, String> createPerson() {
4748 The method must return the value associated to the provided key from the HashMap created
4849 in the createPerson method
4950 */
50-
51+ public String getValue (String getVal ){
52+ return createPerson ().get (getVal );
53+ }
5154
5255
5356 /*
@@ -57,7 +60,9 @@ public HashMap<String, String> createPerson() {
5760 The method must return a boolean that represents whether the string provided exists as a key
5861 in the provided HashMap
5962 */
60-
63+ public boolean hasKey (HashMap <String , String > map , String key ){
64+ return map .containsKey (key );
65+ }
6166
6267
6368 /*
@@ -67,7 +72,16 @@ public HashMap<String, String> createPerson() {
6772 The method must use the string provided to return the integer contained in the provided HashMap,
6873 or -1 if the string provided is not a key in the HashMap
6974 */
75+ public int getValueOrDefault (HashMap <String , Integer > map , String str ){
76+ Object ret ;
77+ ret = map .get (str );
78+ if (ret == null ) {
79+ return -1 ;
80+ }
81+
82+ return (Integer ) ret ;
7083
84+ }
7185
7286
7387 /*
@@ -90,12 +104,18 @@ public ArrayList<String> buildSecretPhrase(ArrayList<Integer> numbers) {
90104 map .put (96 , "nice" );
91105 // Write your code below this comment...
92106
107+ ArrayList <String > strs = new ArrayList <>();
93108
109+ for (int curr : numbers ){
110+ if (map .get (curr ) != null ){
111+ strs .add (map .get (curr ));
112+ }
113+ }
94114
95115
96116 // ...and above this comment
97117
98118 // Change the return statement below to return your actual ArrayList
99- return new ArrayList < String >() ;
119+ return strs ;
100120 }
101121}
0 commit comments