How to change Runnable to lambda expression in Java with IntelliJ shortcut

JavaIntellij IdeaLambda

Java Problem Overview


I have the following anonymous Runnable implementation.

Runnable cronTask = new Runnable() {
       @Override
       public void run() {
          // do something
       }
};

IntelliJ suggests that I replace it with a lambda expression. eg:

Runnable cronTask = () -> {
     // multiple "do something" statements
};

When using a single statement the syntax is:

Runnable cronTask = () -> doSomething();

What shortcut can I use to convert it?

Java Solutions


Solution 1 - Java

Let IntelliJ do the lifting here; you can invoke Alt + Enter (or Option + Return on Mac) to allow IntelliJ to replace it with a lambda expression.

Solution 2 - Java

Alt + Enter is good way to change each one individual

but there is away to change all lambda expression across app

Run Inspection by Name

Ctrl + Shift+ Alt + I

source site

How we can use it in our Android Studio and lambda expression:

just write replace lambda as Inspection name called anonymous type can be replaced with lambda

List of occurrence will appear change each one manually

Tutorial Video

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTechnotronicView Question on Stackoverflow
Solution 1 - JavaMakotoView Answer on Stackoverflow
Solution 2 - JavaMina FawzyView Answer on Stackoverflow