How to change Runnable to lambda expression in Java with IntelliJ shortcut
JavaIntellij IdeaLambdaJava 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
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