I have the following method which takes a list of classes as a parameter:
public List<Interface> getInterfacesOfTypes(List<Class<? extends InternalRadio>> types) {
List<Interface> interfaces = new ArrayList<Interface>();
for(Interface iface : _nodes)
if(types.contains(iface._type))
interfaces.add(iface);
return interfaces;
}
What I want to do is create a wrapper for it where only a single class is specified, which calls the above method with a list of only that one class:
public List<Interface> getInterfacesOfType(Class<? extends InternalRadio> type) {
return getInterfacesOfTypes(Arrays.asList(type));
}
However, I am getting an error:
The method getInterfacesOfTypes(List<Class<? extends InternalRadio>>) in the type InterfaceConnectivityGraph is not applicable for the arguments (List<Class<capture#3-of ? extends InternalRadio>>)
I can’t figure out why this is or what the capture #3-of even means. I’d greatly appreciate any help!
Solution
Change the interface to the following:
To be quite honest, I cannot really explain why. Broadening the range of allowed generic collections (by adding ‘? extends’) just makes it easier for the compiler to see this is valid…
Aside
Arrays.asList(type)I would writeCollections.singletonList(type).Interfaceis not a great name as ‘interface’ is also a Java concept (and it seemsInterfaceis not such an interface 🙂 )Collectionrather than requiring aList