Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard interface for no argument function

Is there in Java 6 (or any compatible library) standard interface for no argument function and generic return type.

Something like:

interface Foo<T> {
      T call();
}
like image 355
Paweł Adamski Avatar asked May 26 '26 13:05

Paweł Adamski


1 Answers

It's a Supplier<T> - it takes nothing, and supplies with T. And the abstract method it defines is nice to be called get()

interface Supplier<T> {
   T get();
}

Note that in Java8, this (@FunctionalInterface) already exists (it's called Supplier), so if you run your code under Java8, there's no need to define a custom interface.

Also, if you run your code under some pre-Java8 version, then you can use the Guava's Supplier interface.

like image 123
Konstantin Yovkov Avatar answered May 30 '26 12:05

Konstantin Yovkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!