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);
}
I have created one class and implemented ConcurrentHashMap
in that class.Now I want to access specific elements of that Map using another method which is in some other class. Please help me.If I pass i value as 10
,it should display AAAA
. Please tell me how to do this.
You should create your other classes with a ConcurrentHashMap
member, and have the ConcurrentHashMap
you created in your CityList
class injected to those classes - either as a constructor argument or call a setter method.
Another option is to have a getter method in your CityList
class, and have other classes access that getter.
You are a long way from where you think you are.
You are declaring and using the Map in he main() method. Local variables are inaccessible to "other classes", although they may be passed to methods/constructors of other classes (and final local variable are accessible to anonymous classes).
What does all that mean to you?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.