What is the Java 8 functional interface for a method that takes nothing and returns nothing?
I.e., the equivalent to the C# parameterless Action
with void
return type?
What is the Java 8 functional interface for a method that takes nothing and returns nothing?
I.e., the equivalent to the C# parameterless Action
with void
return type?
If I understand correctly you want a functional interface with a method void m()
. In which case you can simply use a Runnable
.
java.util.function
package and I completely forgot the old Runnable
–
Palaeozoology Runnable
's interface specification indicates it is to provide executable code to the Thread
class. It doesn't seem right to me to use Runnable
where it's not intended for execution by a thread; seems misleading. Runnable
is defined as a FunctionalInterface
because it meets the specification of a functional interface and can be created using lambda syntax. Why not create your own functional interface? see Runnable, see FunctionalInterface –
Tergum Runnable
is annotated with @FunctionalInterface
and (ii) even in the context of a lambda expression it will be executed on a thread: the thread in which the lambda is run which may be the current thread... –
Clemenciaclemency Runnable
was intended to be used for this purpose, not just for threading contexts. –
Bellamy Runnable
for this purpose in at least one place: Optional::ifPresentOrElse
(Which admittedly also made me think at first if it would run on a different thread) –
Tamah Just make your own
@FunctionalInterface
public interface Procedure {
void run();
default Procedure andThen(Procedure after){
return () -> {
this.run();
after.run();
};
}
default Procedure compose(Procedure before){
return () -> {
before.run();
this.run();
};
}
}
and use it like this
public static void main(String[] args){
Procedure procedure1 = () -> System.out.print("Hello");
Procedure procedure2 = () -> System.out.print("World");
procedure1.andThen(procedure2).run();
System.out.println();
procedure1.compose(procedure2).run();
}
and the output
HelloWorld
WorldHello
Runnable
is the officially documented answer. –
Filefish Note: For a more complete solution, here's an StackOverflow Answer I've posted.
I don't like the decision made by the Java library team to leave out the case of a no-parameter-no-return-value Function
interface, and nudging those who need one to use Runnable
. I think it inaccurately, and even incorrectly, redirects attention and subsequent assumptions when trying to read through a code base. Especially years after the original code was written.
Given communication and readability are far higher concerns for me and my audiences, I have composed a small helper interface
designed and named in the spirit of the Java library's missing case:
@FunctionalInterface
public interface VoidSupplier {
void get();
}
And in my case, the likelihood of an Exception being thrown in the method is considerably higher, so I actually added a checked exception like this:
@FunctionalInterface
public interface VoidSupplier {
void get() throws Exception;
}
@FunctionalInterface
allows only method abstract method Hence you can instantiate that interface with lambda expression as below and you can access the interface members
@FunctionalInterface
interface Hai {
void m2();
static void m1() {
System.out.println("i m1 method:::");
}
default void log(String str) {
System.out.println("i am log method:::" + str);
}
}
public class Hello {
public static void main(String[] args) {
Hai hai = () -> {};
hai.log("lets do it.");
Hai.m1();
}
}
output:
i am log method:::lets do it.
i m1 method:::
© 2022 - 2024 — McMap. All rights reserved.
Supplier<Void>
won't work sincevoid cannot be converted to Void
XD – FilefishRunnable
advice. It miscommunicates intent: https://mcmap.net/q/136591/-java-8-functional-interface-with-no-arguments-and-no-return-value – Antihistamine