Java – HashMap return method
•
Java
I have a method in my class, which initializes a HashMap and puts some keys and values in it, and then the method returns HashMap How to retrieve the returned HashMap?
public Map<String,String> getSensorValue(String sensorName) { registerSensor(sensorName); sensorValues.put("x","25"); sensorValues.put("y","26"); sensorValues.put("z","27"); return sensorValues; }
Here, I call this method from another class:
public static HashMap<String,String> sensValues = new HashMap<String,String>(); AllSensors sensVal = new AllSensors(); sensValues.putAll(sensVal.getSensorValue("orientation")); String something = sensValues.get("x");
But it won't work this way
sensValues.putAll(sensVal.getSensorValue("orientation"));
Crash my android app The point is to backtrack the returned HashMap in some way
Solution
You do not have to copy the map Just try to use the returned reference:
Map<String,String> map = sensVal.getSensorValue("...");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码