How to check key types and values โ€‹โ€‹if Object instanceof HashMap?

I have a method that takes an object. In one case, the method takes a HashMap<String, String> and sets each value to a property of the corresponding key name.

 public void addHelper(Object object) { if (object instanceof HashMap) { HashMap<String, String> hashMap = (HashMap<String, String>) object; this.foo = hashMap.get("foo"); this.bar = hashMap.get("bar"); } } 

This class adheres to a specific interface, so adding settings for these properties is not an option.

My question is: how can I check the type clicked here?

 HashMap<String, String> hashMap = (HashMap<String, String>) object; 

Thanks in advance!

Decision

Thanks to the answer from @drobert, here is my updated code:

 public void addHelper(Object object) { if (object instanceof Map) { Map map = (Map) object; if (map.containsKey("foo")) this.foo = map.get("foo").toString(); if (map.containsKey("bar")) this.bar = map.get("bar").toString(); } } 
+7
java object unchecked-cast
source share
4 answers

You can not. Due to type erasure, reflection will show that you have a HashMap instance, but the types are deleted at runtime. Effectively, you have a HashMap <Object, Object>.

However, you still have some options, and some tips that I suggest you take. Among them:

  • Check to see if there is a Map instance, not a HashMap. This will make your API much more flexible, since you most likely only care that you have quick access by key, and not to any particular impimization.
  • Use the java.util.Map api, which defines "containsKey (Object)" and "get (Object)", so you can still use mapInst.get ("stringKey") safely even without having to do it.
  • You cannot guarantee that all values โ€‹โ€‹are strings, but you can use the java.lang.Object toString () method and get a row for each value independently.

In short: treat it like any card and try to access the keys as Strings, even without a cast, and try to use each value as a String by doing a null check, and then call .toString (), and you will have a much more secure implementation.

+17
source share

It should be noted that the original procedure is exactly equivalent to encoding

 public void addHelper(Object object) { if (object instanceof HashMap) { HashMap hashMap = (HashMap) object; this.foo = (String)(hashMap.get("foo")); this.bar = (String)(hashMap.get("bar")); } } 

An explicit cast (HashMap) cannot cause an error because it is protected by instanceof . Implicitly provided drives (String) will throw an error only if the values โ€‹โ€‹returned from the HashMap are not strings (or zeros).

(By โ€œexactly equivalent,โ€ I mean that the same bytecode is generated.)

+3
source share

You should use try-catch, where you call the addHelper(Object) method. This will provide the correct type of HashMap .

  try{ addHelper(hashMap); } catch(ClassCastException ex){ System.out.println("Is not desired hashmap"); } 
+1
source share
  try{ HashMap<String, String> hashMap = (HashMap<String, String>) object; this.foo = hashMap.get("foo"); this.bar = hashMap.get("bar"); }catch(ClassCastException e){ System.err.log("Object is not a hashmap"); } 

Now you know that the object is of the correct type - even a custom abstract class or otherwise.

0
source share

All Articles