In my code I have multiple instances of List<Future<something>>
and I wanted to have a single method that handles the wait for them to complete. But I get a compiler exception telling me that actual argument List<Future<Boolean>> cannot be converted to List<Future<?>>
.
This is the method head:
public void waitForIt(<List<Future<?>> params)
and this is how it is called:
...
List<Future<Boolean>> actions = new ArrayList<Future<Boolean>>();
waitForIt(actions); <-- compiler error here
...
I need this to work for List<Future<Map<String, String>>>
and several other as well.
Use this:
public void waitForIt(List<? extends Future<?>> params)
When you have List<A>
and List<B>
, A and B must match exactly. Since Future<Boolean>
is not exactly the same as Future<?>
, it does not work.
Future<Boolean>
is a subtype of Future<?>
, but that is not enough. List<A>
is not a subtype of List<B>
even if A is a subtype of B.
We use a wildcard in the type parameter of List
so that it doesn't have to match exactly.
Use this:
public <T> void waitForIt(List<Future<T>> params)
as Future<Boolean>
is not extension of Future<?>
http://ideone.com/tFECPN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With