Incorrect Java Conversion

I have the following line of code

this.htmlSpecialChars = this.getSpecialCharMap();

Where

private HashMap<String,String> htmlSpecialChars;

but I get a warning about an uncontrolled conversion. How to stop this warning?

+3
source share
5 answers

You get this because getSpecialCharMap () returns an object whose type cannot be checked by the compiler as a HashMap <String, String>. Go ahead and provide a prototype for getSpecialCharMap.

+6
source

You get a warning because the compiler cannot verify that the assignment htmlSpecialCharsis HashMap <String, String>, because the getSpecialChars () method returns a normal, non-generic HashMap.

You must change your method to return a specific generic type:

private HashMap<String,String> getSpecialCharMap() {
    return new HashMap<String,String>();
    }
+2

- numberMap - , , , . , :

:

private static HashMap getSpecialCharMap() {
    return new HashMap();
}

public static void main(String[] args) {        
    HashMap<String,String> numberMap = getSpecialCharMap(); //warning
}

:

...
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    @SuppressWarnings("unused")
    HashMap<String,String> numberMap = getSpecialCharMap();
}
0

getSpecialCharMap() HashMap? - Erasure Generics. , @SuppressWarnings("unchecked") getSpecialCharMap() HashMap<String, String>.

-3

:

@SuppressWarnings("unchecked")

.

-6
source

All Articles