如何在java中用lambda替换匿名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37695456/
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 19:34:02  来源:igfitidea点击:

how to replace anonymous with lambda in java

javalambdaanonymous

提问by yukashima huksay

I've got this code but IntelliJ tells me to replace anonymous with lambda but I don't know how. can anyone help me with this? Here is my code:

我有这段代码,但 IntelliJ 告诉我用 lambda 替换匿名,但我不知道如何。谁能帮我这个?这是我的代码:

soundVolume.valueProperty().addListener(new ChangeListener<Number>() {
    public void changed(ObservableValue<? extends Number> ov,
                     Number old_val, Number new_val) {
        main.setSoundVolume(new_val.doubleValue());
        main.getMediaPlayer().setVolume(main.getSoundVolume());
    }
}); 

采纳答案by Davide Lorenzo MARINO

Generally, something like that:

一般来说,像这样:

methodUsingYourClass(new YourClass() {
    public void uniqueMethod(Type1 parameter1, Type2 parameter2) {
        // body of function
    }
});

is replaced with

被替换为

methodUsingYourClass((Type1 parameter1, Type2 parameter2) -> {
    // body of function
});


For your specific code:

对于您的特定代码:

soundVolume.valueProperty().addListener(
       (ObservableValue<? extends Number> ov,
                 Number old_val, Number new_val) -> {
    main.setSoundVolume(new_val.doubleValue());
    main.getMediaPlayer().setVolume(main.getSoundVolume());
});


Notethe replacement of an anonymous class with lambda is possible only if the anonymous class has one method. If the anonymous class has more methods the substitution is not possible.

请注意,仅当匿名类具有一种方法时,才可以用 lambda 替换匿名类。如果匿名类有更多方法,则替换是不可能的。

From oracle documentation:

oracle 文档

The previous section, Anonymous Classes, shows you how to implement a base class without giving it a name. Although this is often more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome. Lambda expressions let you express instances of single-method classes more compactly.

上一节,匿名类,向您展示了如何在不为其命名的情况下实现基类。虽然这往​​往比命名类更简洁,但对于只有一个方法的类来说,即使是匿名类也显得有些过分和繁琐。Lambda 表达式让您可以更紧凑地表达单一方法类的实例

回答by Yassin Hajaj

It doesn't really help with the readability but here it is.

它对可读性没有真正的帮助,但它就在这里。

Note that main's reference should be final or effectively final for this to work.

请注意,main的参考应该是最终的或有效的最终才能使其工作。

soundVolume.valueProperty()
           .addListener(
           (ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> {
               main.setSoundVolume(new_val.doubleValue());
               main.getMediaPlayer().setVolume(main.getSoundVolume())
           });

回答by Per Huss

Why not let IntelliJ show you? Place your cursor on the declaration and press Alt + Enter (or Option + Enter if on a Mac) to bring up the intentions popup. Choose "Replace with lambda" and watch the transformed code...

为什么不让 IntelliJ 向您展示?将光标放在声明上,然后按 Alt + Enter(如果在 Mac 上,则按 Option + Enter)调出意图弹出窗口。选择“用 lambda 替换”并观看转换后的代码...

回答by Fahime Ghasemi

for code clarity, you can also declare a variable with lambda and pass that variable to addListener method. for example

为了代码清晰,您还可以使用 lambda 声明一个变量并将该变量传递给 addListener 方法。例如

soundVolume.valueProperty().addListener(listener);

soundVolume.valueProperty().addListener(listener);

ChangeListener<Number> listener = (observable, oldValue, newValue) -> { ... };

ChangeListener<Number> listener = (observable, oldValue, newValue) -> { ... };

回答by Teri Beckham

You can just right click on the element that gives you the warning, wait until the yellow balloon appears and choose, (replace with Lambda) and Android Studio will do it automatically.

您可以右键单击给您警告的元素,等到黄色气球出现并选择,(替换为 Lambda),Android Studio 会自动执行。