Lambda

Functional interface

  • Definition of a functional interface:
    1. Any interface that contains only one abstract method is a functional interface.
    2. For functional interfaces, we can create objects for that interface through lambda expressions.
  • conclusion
    1. Lambda expressions can only be used if the interface is functional.
    2. When lambda expressions have only one line of code, they can be omitted to one line; otherwise they must be wrapped in code blocks.
    3. When there is only one parameter, you can omit the parameter type and parentheses. When multiple parameters are present, the parameter type can also be omitted, but the parameter type must be omitted at the same time, and must be wrapped with parentheses

Why lambda expressions

  • Avoid too many anonymous inner classes
  • You can make the code look very succinct
  • Cut out a bunch of meaningless code, leaving only the core logic.

Evolution process

package com.lambda; Public class Test01 {//3. Implements static class Like1, implements ILike{@override public void lambda() { System.out.println("I Like Lambda2"); } } public static void main(String[] args) { ILike like = new Like(); like.lambda(); like = new Like1(); like.lambda(); @override public void lambda() {system.out.println ("I Like Lambda3"); } } like = new Like2(); like.lambda(); @override public void lambda() {system.out.println ("I like Lambda4"); }}; like.lambda(); Like = () -> system.out.println ("I like Lambda5"); like.lambda(); } //1. Create interface ILike{void lambda(); {@override public void lambda() {system.out.println ("I Like Lambda1"); }}Copy the code