Java 从不同的类访问 HashMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29197899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing a HashMap from a different class
提问by Mr.Smithyyy
I have a hashmap in my class titled DataStorage:
我的类中有一个名为 DataStorage 的哈希图:
HashMap<String, Integer> people = new HashMap<String, Integer>();
people.put("bob", 2);
people.put("susan", 5);
How can I access the data in this HashMap in a different class?
如何在不同的类中访问此 HashMap 中的数据?
采纳答案by vojta
Create your HashMap as an instance variable and provide a method to access it into your class API:
创建你的 HashMap 作为一个实例变量,并提供一个方法来访问它到你的类 API 中:
public class DataStorage {
private HashMap<String, Integer> people = new HashMap<String, Integer>();
public HashMap<String, Integer> getPeopleMap() {
return people;
}
}
public class AnotherClass {
DataStorage x = new DataStorage();
private void someMethod() {
HashMap<String, Integer> people = x.getPeopleMap();
//work with your map here...
}
}
回答by Aleksander Mielczarek
You can access it:
您可以访问它:
DataStorage storage = new DataStorage();
HashMap<String, Integer> people = storage.people;
回答by AtomHeartFather
You can either make your HashMap public, or create a getter for it:
你可以公开你的 HashMap,或者为它创建一个 getter:
public HashMap<String, Integer> getPeople() {
return people;
}
then you can access it using an instance of the DataStorage class, like this:
然后您可以使用 DataStorage 类的实例访问它,如下所示:
DataStorage dataStorage = new DataStorage();
dataStorage.getPeople()
or, if you also make both the getter and the HashMap static:
或者,如果您还将 getter 和 HashMap 设为静态:
DataStorage.getPeople()
EDIT:
Note, that if your instance variables are not specifically given access modifiers, they default to package
access, which means that they can be accessed from other classes defined in the same package. More details about access modifiers can be found in the documentation
, here's a brief summary:
编辑:请注意,如果您的实例变量没有特别指定访问修饰符,则它们默认为package
access,这意味着可以从同一包中定义的其他类访问它们。可以在 中找到有关访问修饰符的更多详细信息documentation
,这里是一个简短摘要:
Access Levels
访问级别
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
回答by candied_orange
As an advocate of tell don't askI'd like to show how this can be done without any getters.
作为告诉不要问的倡导者,我想展示如何在没有任何吸气剂的情况下做到这一点。
public class TellDontAsk {
interface MapSetter {
public void setMap(Map map);
}
interface MapGiver {
public void giveMap(MapSetter acceptMap);
}
public static void main(String[] args) {
HashMap<String, Integer> people = new HashMap<String, Integer>();
people.put("bob", 2);
people.put("susan", 5);
DataStorage ds = new DataStorage();
ds.setMap(people);
AnotherClass ac = new AnotherClass();
ds.giveMap(ac);
ac.displayMap();
}
public static class DataStorage implements MapSetter, MapGiver {
private Map map;
@Override
public void setMap(Map map) {
this.map = map;
}
@Override
public void giveMap(MapSetter acceptMap) {
acceptMap.setMap(map);
}
}
public static class AnotherClass implements MapSetter {
private Map map;
public void displayMap() {
System.out.println(map);
}
@Override
public void setMap(Map map) {
this.map = map;
}
}
}
Outputs:
输出:
{bob=2, susan=5}
Notice how DataStorage
has no knowlege of AnotherClass
s existence? Nor does AnotherClass
know about DataStorage
. All they share is an interface. This means you're free to do whatever you like in either class so long as you keep supporting that interface.
注意怎么DataStorage
没有AnotherClass
s存在的知识?也不AnotherClass
知道DataStorage
。他们共享的只是一个接口。这意味着只要您继续支持该接口,您就可以在任一类中自由地做任何您喜欢的事情。
BTW, the classes are only static because I was to lazy to move them into their own files. :)
顺便说一句,这些类只是静态的,因为我懒得将它们移动到自己的文件中。:)
回答by Tharindu Bandara
This is eazy
这很容易
public class ListDataStorage {
public static LinkedHashMap getHmapCashType(){
LinkedHashMap<String, String> hMapCashType = new LinkedHashMap<String, String>();
hMapCashType.put("A", "Cash");
hMapCashType.put("B", "Credit");
return hMapCashType;
}
}
access hashmap data from another class
从另一个类访问哈希图数据
String value = ListDataStorage.getHmapCashType().get("A").toString()
回答by Chris Halcrow
If you need to sharethe 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<String,String>
:
如果您需要在应用程序中共享HashMap 的同一个实例,则需要创建一个单例。使用单例保证 HashMap 的同一个实例将始终被任何试图访问它的人引用。例如对于一个HashMap<String,String>
:
Singleton class:
单例类:
public class MyData {
private static final MyData instance = new MyData ();
private MyData () {
HashMap myDataMap = new HashMap<String, String>();
... logic to populate the map
this.referenceData = myDataMap;
}
public HashMap<Integer, DeviceReference> referenceData;
public static DeviceData getInstance(){
return instance;
}
}
Usage in another class:
在另一个类中的用法:
HashMap<String, String> referenceData = MyData.getInstance().referenceData;