How to change Runnable to lambda expression in Jav

2019-03-09 01:00发布

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. What shortcut can I use to convert it? What is the syntax if I would like to do it manually?

Edit: The lambda expression for this Runnable is:

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

2条回答
来,给爷笑一个
2楼-- · 2019-03-09 01:22

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

查看更多
女痞
3楼-- · 2019-03-09 01:28

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.

查看更多
登录 后发表回答