I have the following line of code
this.htmlSpecialChars = this.getSpecialCharMap();
where
private HashMap<String,String> htmlSpecialChars;
but I get a warning about an unchecked conversion. How do I stop this warning?
I have the following line of code
this.htmlSpecialChars = this.getSpecialCharMap();
where
private HashMap<String,String> htmlSpecialChars;
but I get a warning about an unchecked conversion. How do I stop this warning?
Is the return type of
getSpecialCharMap()
non-generic HashMap? Unchecked conversion warning typically happens because of Type Erasure in Generics. In order to get around this, you need to annonate the method with@SuppressWarnings("unchecked")
or change the return type ofgetSpecialCharMap()
toHashMap<String, String>
.You're getting this because getSpecialCharMap() is returning an object whose type cannot be verified by the compiler to be HashMap< String, String>. Go ahead and provide the prototype for getSpecialCharMap.
Preceed the line with:
This will turn off the compiler warning.
You are getting the warning because the compiler cannot verify that the assignment to
htmlSpecialChars
is a HashMap<String,String>, since the method getSpecialChars() is returning a plain, non-generic HashMap.You should modify your method to return the specific generic type:
The best way will be to modify return-type of your method to numberMap's type or this way - please notice this is really bad practice. Don't tell anybody that I showed you this:
Example with unchecked conversion warning:
Example without warning: