javalambdajava-8runnable

Lambda that does absolutely nothing


I needed to have a lambda expression of the functional interface Runnable that did nothing. I used to have a method

private void doNothing(){
    //Do nothing
}

and then use this::doNothing. But I've found an even shorter way to do this.


Solution

  • For Runnable interface you should have something like that:

    Runnable runnable = () -> {};
    

    Where:

    After that, you can call the method

    runnable.run();