[英]how to access ConcurrentHashMap elements from other class Method
public class CityList{
public static void main(String[] args){
ConcurrentHashMap<Integer,String> hm=new ConcurrentHashMap<Integer,String>();
hm.put(10,"AAAA");
hm.put(11."BBBB");
}
}
// another class
public class Getcity extends CityList{
public static void main(String[] args){
public void showcity(int i)
{
system.out.println(hm.get(i);
}
我创建了一个类并在该类中实现了ConcurrentHashMap
。现在,我想使用其他类中的另一种方法访问该Map的特定元素。 请帮助我。如果我将值i传递为10
,则应显示AAAA
。 请告诉我该怎么做。
您应该使用ConcurrentHashMap
成员创建其他类,并将在CityList
类中创建的ConcurrentHashMap
注入到这些类中-作为构造函数参数或调用setter方法。
另一种选择是在CityList
类中具有getter方法,并让其他类访问该getter。
您距离您认为的位置还有很长的路要走。
您正在main()方法中声明和使用Map。 尽管“本地类”可能会传递给其他类的方法/构造函数,但它们无法访问(其他匿名类可以访问最终的本地变量)。
这对您意味着什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.