Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace an anonymous class with a lambda in Java?

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:

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());     } });  
like image 737
yukashima huksay Avatar asked Jun 08 '16 07:06

yukashima huksay


People also ask

What is the difference between lambda expression and anonymous class?

Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.

Does lambda expression eliminates the need of anonymous class?

Lambda expressions are introduced in Java 8. These are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

Which is better lambda expression or anonymous inner class?

Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code. An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.

Can replace any interface in Java with a lambda expression?

Can every anonymous class be replaced by a lambda expression? The answer is no.


1 Answers

Generally, something like this:

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

is replaced with

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

The types of the parameters can be inferred from usage, but there may be situations where specifying them is useful. This part from the above example

(parameter1, parameter2) -> { 

would become this, if you decided to specify the types explicitly

(Type1 parameter1, Type2 parameter2) -> { 

For your specific example, you can use:

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

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

From the oracle documentation:

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.

like image 180
Davide Lorenzo MARINO Avatar answered Oct 05 '22 22:10

Davide Lorenzo MARINO