Suppose a HashMap is defined as:
void defineMap(){
Map options = new HashMap<Object,Object>();
options.put("isValid","true");
options.put("isXYZ","false");
options.put("Size",6.0);
int x = getFromMap(options);
}
Now, when this map is passed to a function and in the signature of the function, the Map is defined as
static int getFromMap(Map<String, String> options) {
String some_number = "Size";
int val=Integer.parseInt(options.get(some_number));
return val;
}
Now, as per my understanding 1 of 2 things should have happened:
Either the java compiler should have thrown an error that no such method found with the same signature since map definition is different All the keys and values defined in the Map should have been converted to String implicitly But none of the above happens as a result of which in my Map, Double(6.0) value is stored with String ("Size") key, which results in ClassCastException as:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
in my getFromMap() function.
Help me understand why no error was thrown while calling the function and passing Map as a parameter or why values were not converted into String.
Why Double value was stored in <String,String> Map
This issue can be easily fixed by type checking for value or converting to string every time. But I want to understand why the above scenario occurred