Lambda that does absolutely nothing

Rien picture Rien · May 7, 2015 · Viewed 22.1k times · Source

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.

Answer

Eddú Meléndez picture Eddú Meléndez · May 8, 2015

For Runnable interface you should have something like that:

Runnable runnable = () -> {};

Where:

  • () because run method doesn't receive args
  • {} body of run method which in this case is empty

After that, you can call the method

runnable.run();