143

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?

deczaloth
  • 7,094
  • 4
  • 24
  • 59
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94

4 Answers4

133

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

Roland
  • 7,525
  • 13
  • 61
  • 124
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 3
    Yes, I was looking for it in the new `java.util.function`package and I completely forgot the old `Runnable` – Miguel Gamboa May 26 '14 at 11:51
  • 50
    `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](http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html), see [FunctionalInterface](http://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html) – TheSecretSquad May 01 '15 at 04:02
  • 9
    @TheSecretSquad (i) `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... – assylias May 01 '15 at 08:36
  • 12
    I agree with @TheSecretSquad, while it satisfies the functional requirement, it does not sound very semantic, Runnable is commonly associated with creating threads. A simple Worker functional interface with a doWork method would have been nice. EDIT: Oops: http://stackoverflow.com/questions/27973294/function-with-no-args-and-no-return-type-void-in-java-8?lq=1#comment44339647_27973294 – jpangamarca Aug 19 '15 at 15:46
  • 10
    Fleshing out @jpangamarca's "Oops" above: In the comments on the duplicate question he linked, Brian Goetz confirmed that `Runnable` was intended to be used for this purpose, not just for threading contexts. – Joshua Goldberg Mar 31 '16 at 21:26
  • 12
    Given that that's the case, though, I think the javadoc should be made more generic: https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html – Joshua Goldberg Mar 31 '16 at 21:29
  • 1
    If you end up writing lambdas without parameters and return values, you are likely to write code with side effects. So it's time to think over and change your design. – BetaRide Mar 20 '19 at 12:16
  • 3
    FWIW Java 11 standard library uses a `Runnable` for this purpose in at least one place: [`Optional::ifPresentOrElse`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html#ifPresentOrElse(java.util.function.Consumer,java.lang.Runnable)) (Which admittedly also made me think at first if it would run on a different thread) – aksh1618 Jun 01 '20 at 15:06
  • 3
    since Java 19, it's actually the *official* way: see https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/Runnable.html vs Java 18's https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Runnable.html – spamove Jun 27 '23 at 11:50
34

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
Charana
  • 1,074
  • 1
  • 13
  • 26
1

@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:::
Vladislav Varslavans
  • 2,775
  • 4
  • 18
  • 33
uma mahesh
  • 131
  • 1
  • 5
0

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;
}
chaotic3quilibrium
  • 5,661
  • 8
  • 53
  • 86