Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot use List<Future<?>> in method parameter when called from different places

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.

like image 932
Angelo Fuchs Avatar asked Nov 07 '12 14:11

Angelo Fuchs


Video Answer


2 Answers

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.

like image 188
newacct Avatar answered Sep 18 '22 01:09

newacct


Use this:

public <T> void waitForIt(List<Future<T>> params)

as Future<Boolean> is not extension of Future<?>

http://ideone.com/tFECPN

like image 28
guido Avatar answered Sep 19 '22 01:09

guido