Reputation: 3278
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);
}
}
Upvotes: 0
Views: 148
Reputation: 11027
In your test()
function, there is no way for the compiler to determine that this specific instance is a <String, Integer>
. So the compile error.
For example, this would work:
public static void main(String [] args) {
FormattedMap<String, Integer> fm = new FormattedMap<>(); // This is fine
// fm.test(true);
fm.addMapValue("first", new Integer(2));
}
If test()
was static
, the instance being parameterized and passed as a parameter, it would work too:
public static void test(FormattedMap<String, Integer> instance) {
instance.addMapValue("first", new Integer(2));
}
The point is that you are currently assuming that the generic types are String
an Integer
in a piece of code that expects these types to be generic.
Upvotes: 1
Reputation: 692181
Let's forget about your main method. The code for the class is thus
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
}
}
So, your class defines a test()
method, which calls the method addMapValue(T_KEY key, T_VALUE value)
with a String and an Integer as argument. Given that your class is generic, the generic types could be anything. Not necessarily String and Integer. So this method can't compile.
Moreover, your class also defines two fields mapKey and mapValue that are completely useless.
The code should instezad be:
public class FormattedMap<T_KEY, T_VALUE> extends LinkedHashMap<T_KEY, ArrayList<T_VALUE>> {
public FormattedMap() {
super();
}
public void addMapValue(T_KEY key, T_VALUE value) {
}
public void removeMapValue(T_KEY key, T_VALUE value) {
}
public static void main(String [] args) {
FormattedMap<String, Integer> fm = new FormattedMap<>(); // This is fine
fm.addMapValue("first", new Integer(2));
// this is valid, because fm is of type FormattedMap<String, Integer>
}
}
Note that extending LinkedHashMap is most certainly a bad idea anyway. Your class should HAVE a LinkedHashMap, instead of BEING a LinkedHashMap.
Upvotes: 5
Reputation: 1
This is because of incompatible function signature addMapValue("first", new Integer(2));
Upvotes: 0