“This is the 10th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

1. Why lambda expressions?

  • Avoid overdefining anonymous inner classes
  • You can make your code look neat
  • Get rid of a bunch of code that doesn’t make sense, leaving only the core logic.

Add: Functional interfaces are key to lambdab expressions.

Definition of a functional interface: Any interface that contains only one abstract method is a functional interface. Public interface Runnable{pulic abstract void run(); }. We can use lambda expressions to create objects for that interface

2. Lambda simplification process

Note: Lambda expressions are a new feature in Java 8. To accommodate people’s needs and simplify code, the premise is that a functional interface is needed.

Simplified order of lambda: external class -> static inner class -> local inner class -> Anonymous inner class ->lambda

Concrete implementation:

  • Start by defining a functional interface

Define a functional interface ILike with a lambda() method.

  • External class implementation methods

Create an external Like class that implements the ILike interface and outputs a message. Main method test: Create a Like object to call the lambda() method.

  • Static inner class

Create a static inner class Like2 that implements the ILike interface. The main method test: Create an object for Like2 to call the lambda method.

  • Local inner class

Create a local inner class Like3 that implements the ILike interface. The main method test: create an object for Like3 to call the lambda method.

  • Anonymous inner class

Anonymous inner class, gets the class object directly, calling the lambda() method.

  • Lambda simplified

Lambda gets the object directly in the form of a different expression, calling the lambda() method.

3. Simplified form of lambda

Basic grammar:

(): Describes the parameter list

{} : used to describe method bodies can sometimes be omitted

The Lambda operator is read as goes to

Simplifying rules:

I’m going to say it in parametric form.

1. Simplify parameter types

You can omit argument types, but must omit all arguments:

2. Simplify parameter curly braces

Parameter parentheses can be omitted if there is only one argument:

3. Simplified method body braces

  • If the method body has only one statement, you can omit the method body braces

  • If the only statement in the method body is a return statement, the return must be omitted as well as the braces.