Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 functional interface with no arguments and no return value

People also ask

Which functional interface does not return a value?

Consumer. The Java Consumer interface is a functional interface that represents an function that consumes a value without returning any value.

Can functional interface be empty?

You can use this type of interfaces. I would like to start with the conditions where you need this type of construct. Default empty methods can be used to make implementation of any method optional. However, you can make a abstract adaptor class of such interface with a default implementation.

Which built in functional interface represents an operation that takes an argument but returns nothing?

Consumer. A consumer is an operation that accepts a single input argument and returns no result; it just execute some operations on the argument.

What functional interface suits a lambda expression that accepts a string and returns nothing?

For example, the Runnable interface can be viewed as the signature of a function that accepts nothing and returns nothing ( void ) because it has only one abstract method called run , which accepts nothing and returns nothing ( void ).


If I understand correctly you want a functional interface with a method void m(). In which case you can simply use a Runnable.


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