I have a simple set of concrete classes that all extend the same interface.
I want to pass in an optional list of objects into a method, so my method signature looks roughly like this:
void method(Optional<List<? extends AnInterface> arg);
When I create an argument using a concrete type, Concrete1, which implements AnInterface and use it in the method call:
List<Concrete1> list = new ArrayList<>();
Optional<List<Concrete1>> opt = Optional.of(list);
ex.method(opt);
I get the following error:
'method(Optional<List<? extends AnInterface>>)' in 'GenericArgBoundsExample' cannot be applied to '(Optional<List>)'
Because Concrete1 implements AnInterface I would think the method should be applicable to the argument Optional<List>.
If I do the same thing without the Optional, "method(List<? extends AnInterface>)", there are no issues.
Is it possible to define the method in such a way that I can pass in this type of argument?
Below is a stripped down test class that I use to generate this error.
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
interface AnInterface {
void aMethod();
}
class Concrete1 implements AnInterface {
@Override
public void aMethod() { /* Do nothing. */ }
}
class Concrete2 implements AnInterface {
@Override
public void aMethod() { /* Do nothing. */ }
}
public class GenericArgBoundsExample {
public void takesAList(List<? extends AnInterface> arg) { /* Do nothing. */ }
public void takesAnOptional(Optional<? extends AnInterface> arg) { /* Do nothing. */ }
public void takesAnOptionalList(Optional<List<? extends AnInterface>> arg) { /* Do nothing. */ }
public static void main() {
GenericArgBoundsExample ex = new GenericArgBoundsExample();
List<Concrete1> listConcrete = new ArrayList<>();
Optional<Concrete1> optConcrete = Optional.of(new Concrete1());
Optional<List<Concrete1>> optListConcrete = Optional.of(listConcrete);
// Ok
ex.takesAList(listConcrete);
// Ok
ex.takesAnOptional(optConcrete);
// Not Ok
ex.takesAnOptionalList(optListConcrete);
// Error message:
// 'takesAnOptionalList(Optional<List<? extends AnInterface>>)' in
// 'GenericArgBoundsExample' cannot be applied to '(Optional<List<Concrete1>>)'
}
}
> arg)`