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(); } }
java object unchecked-cast
Shaun scovil
source share