M
MrFredBloggs
import java.util.HashMap;
public class GenericMapTest
{
private HashMap<String, ? > map = null;
private char type = '\0';
public GenericMapTest(char charIn)
{
super();
switch (charIn)
{
case 'S':
type = charIn;
map = new HashMap<String, String>();
break;
case 's':
type = 'S';
map = new HashMap<String, String>();
break;
case 'I':
type = charIn;
map = new HashMap<String, Integer>();
break;
case 'i':
type = 'I';
map = new HashMap<String, Integer>();
break;
}
}
public void add(String stringIn)
{
switch (type)
{
case 'I':
//BOUND MISMATCH: The method put(String, ?) of type
//HashMap<String, ?> is not applicable for the
arguments
//(String, Integer). The wildcard parameter ? has no
lower
//bound and may actually be more restrictive than
argument
//Integer.
map.put(stringIn, new Integer(stringIn.trim()));
break;
case 'S':
//BOUND MISMATCH: The method put(String, ?) of type
//HashMap<String, ?> is not applicable for the
arguments
//(String, String). The wildcard parameter ? has no
lower
//bound and may actually be more restrictive than
argument
//String.
map.put(stringIn.trim(), stringIn.trim());
break;
}
}
}
I'm new to generics.
Can the above errors be solved using the new Java5 generic coding
techniques or is what I am trying to do just too silly in the first
place.
Regards,
Fred.
public class GenericMapTest
{
private HashMap<String, ? > map = null;
private char type = '\0';
public GenericMapTest(char charIn)
{
super();
switch (charIn)
{
case 'S':
type = charIn;
map = new HashMap<String, String>();
break;
case 's':
type = 'S';
map = new HashMap<String, String>();
break;
case 'I':
type = charIn;
map = new HashMap<String, Integer>();
break;
case 'i':
type = 'I';
map = new HashMap<String, Integer>();
break;
}
}
public void add(String stringIn)
{
switch (type)
{
case 'I':
//BOUND MISMATCH: The method put(String, ?) of type
//HashMap<String, ?> is not applicable for the
arguments
//(String, Integer). The wildcard parameter ? has no
lower
//bound and may actually be more restrictive than
argument
//Integer.
map.put(stringIn, new Integer(stringIn.trim()));
break;
case 'S':
//BOUND MISMATCH: The method put(String, ?) of type
//HashMap<String, ?> is not applicable for the
arguments
//(String, String). The wildcard parameter ? has no
lower
//bound and may actually be more restrictive than
argument
//String.
map.put(stringIn.trim(), stringIn.trim());
break;
}
}
}
I'm new to generics.
Can the above errors be solved using the new Java5 generic coding
techniques or is what I am trying to do just too silly in the first
place.
Regards,
Fred.