如何使用 IntelliJ 快捷方式在 Java 中将 Runnable 更改为 lambda 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34048468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:15:42  来源:igfitidea点击:

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

javaintellij-idealambda

提问by Technotronic

I have the following anonymous Runnableimplementation.

我有以下匿名Runnable实现。

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

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

IntelliJ 建议我用 lambda 表达式替换它。例如:

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?

我可以使用什么快捷方式来转换它?

采纳答案by Makoto

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

让 IntelliJ 来完成这里的工作;您可以调用Alt+ Enter(或Mac 上的Option+ Return)以允许 IntelliJ 将其替换为 lambda 表达式。

回答by Mina Fawzy

Alt + Enteris good way to change each one individual

Alt +Enter是改变每个人的好方法

but there is away to change all lambda expression across app

但是可以跨应用程序更改所有 lambda 表达式

Run Inspection by Name

Ctrl+ Shift+ Alt + I

Ctrl+ Shift+ Alt +I

source site

源站

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

我们如何在我们的 Android Studio 和 lambda 表达式中使用它:

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

只需将替换 lambda 写为调用的检查名称 anonymous type can be replaced with lambda

List of occurrence will appear change each one manually

出现的列表会出现手动更改每一个

Tutorial Video

教程视频