0

So, the problem is the following. I have a method which I want to make it work for Maps and for JSONObject, so it can be called in both cases.

After some operations, I need to put into that Map/JSONObject an Object. I am trying to do this:

private void putDefaultValue(Object obj) {
    //... some code...
    String key = "key";
    Object objectToUpdate = ...; //whatever
    //... some code...
    if (obj instanceof Map) ((Map<?,?>)obj).put(key, objectToUpdate);
    else if (obj instanceof JSONObject) ((JSONObject)obj).put(key, objectToUpdate);
}

The JSONObject does not give any problem, but the Map casting forces the error:

The method put(capture#6-of ?, capture#7-of ?) in the type Map is not applicable for the arguments (String, Object)

I can change the Map<?,?> casting for Map<String,Object> but then I get a warning:

Type safety: Unchecked cast from Object to Map

I would not like to add any suppress warnings, and be able to do this in a propper way. Any suggestions? Thanks

Mayday
  • 4,680
  • 5
  • 24
  • 58

0 Answers0