0

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


Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • A raw type warning **was** emitted when you used the type Map, without specifying its generic type. Define it as Map, and you won't be able to insert anything other than a String in it. – JB Nizet Apr 26 '19 at 07:24
  • Generics is not about converting objects to other types. That will never happen. It's about specifying which types are allowed. – JB Nizet Apr 26 '19 at 07:26
  • `Map options` - you're using a rawtype. If you ignore compiler warnings you cannot then complain that you weren't warned! – Boris the Spider Apr 26 '19 at 07:27

2 Answers2

0

Answering your Q. Why Double value was stored in Map

You have created Map so you can put any datatype object in this map when you put only 6.0 it will behave as double it you put this as "6.0" its class type is String like demo

            Object obj="6.0";
            System.out.println("type is ="+obj.getClass());

            obj=6.0;
            System.out.println("type is ="+obj.getClass());

            obj=6;
            System.out.println("type is ="+obj.getClass());

output:

type is =class java.lang.String
type is =class java.lang.Double
type is =class java.lang.Integer

so better to use Object return type.

-2

Try use the Double.intValue(); https://www.tutorialspoint.com/java/lang/double_intvalue.htm

int val= options.get(some_number).intValue();

The double wasnt stored in a <String,String> Map, you get the Exception because Integer.parseInt expectes a String and that where the ClassCastException is thrown

spinhaxo
  • 58
  • 6