Miguel Gamboa
Miguel Gamboa

Reputation: 9373

Java 8 functional interface with no arguments and no return value

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?

Upvotes: 150

Views: 41475

Answers (5)

JBoy
JBoy

Reputation: 5735

Create your own functional interface...

public interface Executable {
  void exec();
}

As Pierre-Yves Saumont says in his book Functional programming in Java ....
"You could have used the standard Runnable interface, but most code verifiers raise a warning if this interface is used for something other than running a thread."

Upvotes: 0

chaotic3quilibrium
chaotic3quilibrium

Reputation: 5924

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;
}

Upvotes: 2

uma mahesh
uma mahesh

Reputation: 124

@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:::

Upvotes: 1

Charana
Charana

Reputation: 1072

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

Upvotes: 33

assylias
assylias

Reputation: 328618

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

Upvotes: 137

Related Questions