I'm looking to create a method which will accept a String
as a parameter and then print out the respective HashMap
that the String
refers too.
sensorMappings = new HashMap<>();
sensorMappings.put(136, "doorNumber");
This is my method to print the HashMap
:
void printHashMap(String mapChoice){
for (Integer ID : mapChoice.keySet()) {
String key = ID.toString();
String value = sensorMappings.get(ID);
System.out.println(key + " " + value);
}
}
I am getting the error:
Can't resolve method 'keySet()'
EDIT - full code:
Main.class
sensorInfo.printHashMap("mapChoice");
Sensor.class
public class Sensors {
private HashMap<Integer, String> sensorMappings;
Sensors(){
sensorMappings = new HashMap<>();
sensorMappings.put(136, "doorNumber");
}
void printHashMap(String mapChoice){
for (Integer ID : mapChoice.keySet()) {
String key = ID.toString();
String value = sensorMappings.get(ID);
System.out.println(key + " " + value);
}
}
I have multiple HashMaps and wish to create a generic method to print them by passing in the required HashMap.
sensorMappings.keySet()
instead? The latter should not give any compile errors.