I have a hashmap in my class titled DataStorage:
HashMap people = new HashMap();
people.put(\"bob\", 2);
peopl
If you need to share the same instance of a HashMap across your application, you need to create a singleton. Using a singleton guarantees that the same instance of the HashMap will always be referenced by anything trying to access it. For example for a HashMap
:
Singleton class:
public class MyData {
private static final MyData instance = new MyData ();
private MyData () {
HashMap myDataMap = new HashMap();
... logic to populate the map
this.referenceData = myDataMap;
}
public HashMap referenceData;
public static DeviceData getInstance(){
return instance;
}
}
Usage in another class:
HashMap referenceData = MyData.getInstance().referenceData;