I’m getting several compiler warnings about my use of generics in one class and could use some help. I have the following setup:
Map<EventType, List<? extends Listener>> map;
I have several different event types and for each one I want to keep a list of the corresponding listener type. Listener is an interface and there are several interfaces that extend it.
Later on I add a List to this map like:
List<StateListener> list = new LinkedList<StateListener>();
list.add(someStateListener);
map.put(Events.STATE, list);
And then I get warnings later on …
List<StateListener> list = map.get(Events.STATE);
for unchecked conversion.
I read through some other posts but I didn’t really see anything that helped. Thanks any comments
There’s no guarantee that the
Listcoming frommap.get(Events.STATE)is aList<StateListener>since you declared the map with the value type ofList<? extends Listener>You need to cast it, or declare accordingly;