Skip to main content
  1. About
  2. For Teams
Asked
Viewed 220 times
0

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.

7
  • 1
    you pass a String not a Map to printHashMap. keySet cannot work.
    nano_nano
    –  nano_nano
    2017-01-11 11:45:53 +00:00
    Commented Jan 11, 2017 at 11:45
  • could you post the whole code, so that we can try to correct?
    sameera sy
    –  sameera sy
    2017-01-11 11:49:23 +00:00
    Commented Jan 11, 2017 at 11:49
  • I've added the additional code.
    Colin747
    –  Colin747
    2017-01-11 11:54:52 +00:00
    Commented Jan 11, 2017 at 11:54
  • What is the desired result? Is it different from what you would obtain if you used sensorMappings.keySet() instead? The latter should not give any compile errors.
    Anonymous
    –  Anonymous
    2017-01-11 11:58:20 +00:00
    Commented Jan 11, 2017 at 11:58
  • I have multiple hashMaps and want a generic method to print any of them that have been passed.
    Colin747
    –  Colin747
    2017-01-11 11:58:49 +00:00
    Commented Jan 11, 2017 at 11:58

3 Answers 3

2

You can achieve this if you register all your various hashMaps in another variable keyed by name. Then you can look up any of the hash maps that you've got registered.

An example would be below.

public class MapRegistry {
  static Map<String,HashMap<Integer,String>> allMaps = new HashMap<>();

  public static void register(String name, HashMap<Integer,String> myMap) {
    allMaps.put(name, myMap);
  }

  public static HashMap<Integer,String> find(String name) {
    return allMaps.get(name);
  }
}

In your sensors class

Sensors(){
  sensorMappings = new HashMap<>();
  MapRegistry.register("sensorMappings", sensorMappings);
  sensorMappings.put(136, "doorNumber");
}

Then to look up the particular map:

void printHashMap(String mapChoice){
  HashMap<Integer,String> map = MapRegistry.find(mapChoice);
  for (Integer ID : map.keySet()) {
    String key = ID.toString();
    String value = map.get(ID);
    System.out.println(key + " " + value);
  }
}

You don't need a static class if you are prepared to pass the registry around or can inject it.

You will need to pay attention if you ever remove maps as the registry will keep hold of them. So you'll need to ensure that the registry is updated as well or you'll get leaks.

Sign up to request clarification or add additional context in comments.

5 Comments

I am getting some errors: HashMap map = MapRegistry.get(mapChoice); - cannot resolve method get Integer ID - required Object, foundIinteger String value = map.get(ID); - required String, found Object
Gary - Stand with Ukraine
@ColinShewell I've added the parameter types to the printHashMap function.
I'm still getting the HashMap<Integer, String> map = MapRegistry.get(mapChoice); gives the error cannot resolve method get(Java.lang.String)
.get should be .find
what would the best way to update the registry if records are modified?
1

No you cannot. Java is a strongly typed Language. You have to pass the map and not String to compile it. Maybe you can have a look at the reflection API (not suggested unless you have a string reason to use it).

2 Comments

"unless you have a string reason to use it" Pun intended ? :-)
@Tim You got me
0

You are not passing the hashmap at all. You are passing a string. Maybe you are trying to pass a hashmap than a string.

printHashMap(sensorMappings);

In the print function.

void printHashMap(HashMap mapChoice){
    for (Integer ID : mapChoice.keySet()) {
        String key = ID.toString();
        String value = sensorMappings.get(ID);
        System.out.println(key + " " + value);
    }
}

If you are trying to encode the HashMap and convert that back to the string then you can try this

String value = sensorMappings.toString();
value = value.substring(1, value.length()-1);           //remove curly brackets
String[] keyValuePairs = value.split(",");              //split the string to creat key-value pairs
Map<String,String> map = new HashMap<>();               

for(String pair : keyValuePairs)                        //iterate over the pairs
{
    String[] entry = pair.split("=");                   //split the pairs to get key and value 
    map.put(entry[0].trim(), entry[1].trim());          //add them to the hashmap and trim whitespaces
}

2 Comments

Looks like OP know this and he is trying to create a generic method.
Your first code example looks really untested and flawed? How about rechecking it?

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.