I am trying to define my first generic class. I want it to extend a HashMap. It is a LinkedHashMap from which the key is generic type, and the value is an ArrayList of generic-type too.
Building an instance of this class is ok. However when I want to add values, then the compiler says
incompatible types: String cannot be converted to T_KEY addMapValue("first", new Integer(2)); where T_KEY is a type-variable: T_KEY extends Object declared in class FormattedMap I guess this may be due to the fact that my variables T_KEY and T_VALUE aren't initialized? How can I initialize them?
Here is my class :
public class FormattedMap<T_KEY, T_VALUE> extends LinkedHashMap<T_KEY, ArrayList<T_VALUE>> { private T_KEY mapKey; private T_VALUE mapValue; public boolean DEBUG=false; public FormattedMap() { super(); } public void addMapValue(T_KEY key, T_VALUE value) { } public void removeMapValue(T_KEY key, T_VALUE value) { } public void test(boolean b) { addMapValue("first", new Integer(2)); // This triggers the compilor error message } public static void main(String [] args) { FormattedMap<String, Integer> fm = new FormattedMap<>(); // This is fine fm.test(true); } }