Nilamber
Nilamber

Reputation: 87

Unable to retrieve all the key for a Map(HashMap)?

I am working on a client-server application in which server sends the indication and client receives those indication.The Map is declared as

private static Map<key,value> obj=new HashMap<key,value>();

The Map is getting the values through synchronised(obj) in constructor of the value Class

obj.put(this,this);

I am trying to retrieve the all the keys for this Map. I used Set<key> t=Map.KeySet(); but this throws a compile error saying can not found symbol :KeySet() in interface java.util.Map. Note:key is interface and value is the class in which Map is Declared.

Upvotes: 0

Views: 136

Answers (3)

Garbage
Garbage

Reputation: 1520

As you have written

private static Map<key,value> obj=new HashMap<key,value>();

You should write

Set<key> t=obj.keySet();

instead of Set<key> t=Map.KeySet();

Upvotes: 1

Vinay Veluri
Vinay Veluri

Reputation: 6865

Check the Map API once. Map is interface and you are trying to use the keySet() like you call a static method.

You should call the methods on the object created.

you should use obj.keySet()

Map Api

Upvotes: 0

parkydr
parkydr

Reputation: 7782

There is no KeySet method on Map, keySet is the correct method for getting the keys

Upvotes: 0

Related Questions