I have the following code
private Map<KEY, Object> values = new HashMap<KEY, Object>();
public void set(KEY key, Object value) {
values.put(key, value);
}
private Object getObj(KEY key) {
return values.get(key) == null ? key.getDefaultValue() : values.get(key);
}
public List<E> getList(KEY key) {
return (List<E>) getObj(key);
}
The idea to be able to send in any Objects in a map and then retrieve it, but how do I solve the genereics-part in java?
Here is an example code. Here I save the List into the map:
List<String> list = new ArrayList<String>();
list.add("a string");
session.set(KEY.LIST, list);
Now I want to get the List again:
List<String> list = session.getList(KEY.LIST);
But now I get a
cannot convert from List<E> to List<String>
error for obvious reasons.
Is it possible to get a List(String) from the method, without any typecasts? Is this method correct?
public List<E> getList(KEY key) {
return (List<E>) getObj(key);
}