Suppose there is a requirement to use a particular generic interface, but the situation does not require making use of one of the generic parameters.
Let's say I need a Callable<T>
(which must return a T
from its call()
method), but on this occasion I don't need the return result, I just want to submit some code to an ExecutorService
to "do something"
What's the best option for the type T
?
You can use the special Void
type:
Callable<Void> callable = new Callable<Void>() {
@Override
public Void call() throws Exception {
// do stuff
return null;
}
};
The return
statement is required to exit the method. The only value which the compiler accepts is null
. Quite handy!
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