New features of Java 8 lambda expression

A functional interface is an interface that explicitly declares only one abstract method. To ensure that the number of methods is small, java8 provid...
Combination of Lambda expression and functional interface
Other functional interfaces and their implementation

A functional interface is an interface that explicitly declares only one abstract method. To ensure that the number of methods is small, java8 provides a special annotation @ functional interface, so that when there is more or less than one abstract method declared in the interface, an error will be reported. As shown in the figure below:

Lambda expression

Lambda expressions are essentially anonymous methods. Let's look at the following example:

public int add(int x, int y) { return x + y; }

This is what it looks like when converted to a Lambda expression:

(int x, int y) -> x + y;

Parameter types can also be omitted. The Java compiler infers from the context:

(x, y) -> x + y; //Return the sum of two numbers

perhaps

(x, y) -> { return x + y; } //Explicitly indicating the return value

It can be seen that Lambda expression consists of three parts: parameter list, arrow (- >), and an expression or statement block.

Combination of Lambda expression and functional interface

Steps:

  1. Create a new nonparametric functional interface (demonstrate nonparametric interface first);
  2. Create a new class with a functional interface attribute;
  3. Implement functional interface;
  4. Method of testing functional interface;
@FunctionalInterface public interface InterfaceWithNoParam { void run1(); }

//Anonymous Inner Class InterfaceWithNoParam param1 = new InterfaceWithNoParam() { @Override public void run1() { System.out.println("Implemented by anonymous inner class run()"); } }; //Lambda expression //Empty parentheses indicate no parameters InterfaceWithNoParam param = () -> System.out.println("adopt Lambda Expression implementation run()") ;
@Test public void testIntfaceWithNoparam() { this.param.run1(); this.param1.run1(); }

Other functional interfaces and their implementation

The above content implements the function interface and implementation of no parameter and no return value, and other forms of course:

  1. Return value with parameter or not
  2. No parameter return value
  3. With parameter and return value

Here is an example of a test I wrote:

package lambda; @FunctionalInterface public interface InterfaceWithParams { void run(String param); }
package lambda; @FunctionalInterface public interface InterfaceWithNoParam { void run1(); }
package lambda; @FunctionalInterface public interface InterfaceUnVoidWithParams { String run(String param); }
package lambda; import org.junit.Test; public class TestJava8 { //Anonymous Inner Class InterfaceWithNoParam param1 = new InterfaceWithNoParam() { @Override public void run1() { System.out.println("Implemented by anonymous inner class run()"); } }; //Lambda expression //Empty parentheses indicate no parameters InterfaceWithNoParam param = () -> System.out.println("adopt Lambda Expression implementation run()") ; @Test public void testIntfaceWithNoparam() { this.param.run1(); this.param1.run1(); } // lambda InterfaceWithParams interfaceWithParams = (String param) -> System.out.println("lambda Expression has parameters and no return value," + param) ; // Anonymous Inner Class InterfaceWithParams interfaceWithParams02 = new InterfaceWithParams() { @Override public void run(String param) { System.out.println("Anonymous inner class expression has parameters and no return value," + param); } }; @Test public void testIInterfaceWithParam(){ interfaceWithParams02.run("Anonymous Inner Class "); interfaceWithParams.run("lambda"); } // lambda has parameter and return value // Interfaceunvoidwithparams interfaceunvoidwithparams = (string s) - > ; InterfaceUnVoidWithParams interfaceUnVoidWithParams = (String s) -> "What you entered is" + s; InterfaceUnVoidWithParams interfaceUnVoidWithParams01 = new InterfaceUnVoidWithParams() { @Override public String run(String s) { return "What you entered is" + s; } }; @Test public void testInterfaceUnVoidWithParams(){ String a = interfaceUnVoidWithParams.run("lambda"); System.out.println(a); String b = interfaceUnVoidWithParams01.run("Anonymous Inner Class "); System.out.println(b); } }

30 November 2019, 18:52 | Views: 1995

Add new comment

For adding a comment, please log in
or create account

0 comments