An overview of the

Lambda expressions are a new feature in Java8. You can think of it as an anonymous function.

Lambda expressions can be understood as condenses a function into a single line of code, making the code more compact and concise.

Lambda expression syntax:

(parameters) -> statement;

or

(parameters) -> {statements; }

The parameters are as follows:

  • Parameters: a parameter that can be specified or not specified. Parentheses can be omitted when there is only one parameter
  • Statement: The value returned by the function is directly returned without curly braces
  • Statements: the body of the function, which needs to indicate the value returned by the expression when braces are used

Examples of lambda expressions:

// Return 5 () -> 5; // Return 2*x x -> 2*x; // Accept the argument x y, return x+y (x, y) -> x+y; // specify the receiving parameter type (int x, int y) -> x + y; (String s) -> system.out.print (s); Print (s) -> {system.out.print (s); return s; };Copy the code

Lambda expressions are used in Java

There is in Python. But in Python, where everything is an object and you simply assign a function to a variable, how do you use lambda expressions in Java?

 

The results

Lambda expressions implement methods in interfaces. How do they feel like anonymous inner classes?

Lambda expressions differ from anonymous inner classes:

  1. This keyword. This in an anonymous inner class refers to the current anonymous class, and this in a lambda expression refers to the lambda outer class.

FunctionInterface comments:

@functionInterface is a new Java8 interface that indicates that the interface is a functional interface defined according to the Java language specification. For example:

@FunctionInterface
public interface MathOperation{
 public int operation(int a, int b);
}
Copy the code

If you add more abstract methods to the interface, a compilation error will be thrown.

Variables in lambda expressions

 

That’s fine, but if you want to modify the string later, what’s the problem

 

As you can see, external local variables used by lambda expressions must be final. What about member variables?

 

Member variables and static variables can be used and modified later, for reasons explained in this article

Java inner class

Examples of lambda expressions in Java

So what are the benefits of introducing lambda expressions in Java? Here are a few examples:

1. Create a thread

 

2. To traverse the list

 

Of course, there are many more, you can try it yourself

Java8 functional interface

A functional interface is an interface that has one and only abstract method, but can have multiple non-abstract methods.

Lambda expressions are well supported by functional interfaces.

Before JDK1.8, there were some functional interfaces:

  • java.lang.Runable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

New functional interfaces in JDK1.8:

The java.util.function package contains a number of classes to support Java functional programming. The functional interfaces to this package include:

  1. BiConsumer

    : represents an operation that takes two input parameters and returns no results
    ,u>
  2. BiFunction

    : represents a method that takes two input parameters and returns a result
    ,u,r>
  3. BinaryOperator

    : represents an operation that operates on two operators of the same type and returns the result of the operator of the same type
  4. BiPredicate

    : represents a two-parameter Boolean value method
    ,u>
  5. BooleanSupplier: represents the provider of the Boolean result
  6. Consumer

    : represents an operation that takes an input parameter and returns nothing
  7. DoubleBinaryOperator: Represents an operation that operates on two double operators and returns the result of a double.
  8. DoubleConsumer: Represents an operation that takes a double and returns no result.
  9. DoubleFunction

    : represents a method that takes a double and returns the result
  10. DoublePredicate: Represents a Boolean method that takes a double parameter
  11. DoubleSupplier: represents the provider of a double value structure
  12. DoubleToIntFunction: Takes a double and returns an int.
  13. DoubleToLongFunction: Takes a double and returns a long
  14. DoubleUnaryOperator: Takes an argument of type double and returns a value of type double.
  15. Function

    : takes an input argument and returns a result.
    ,r>
  16. IntBinaryOperator: Accepts both arguments of type int and the return value of type int.
  17. IntConsumer: Accepts an input parameter of type int, with no return value.
  18. IntFunction

    : takes an input parameter of type int and returns a result.
  19. IntPredicate: Takes an int input parameter and returns the result of a Boolean value.
  20. IntSupplier: Returns an int result with no argument.
  21. IntToDoubleFunction: Takes an int and returns a double.
  22. IntToLongFunction: takes an int input and returns a long result.
  23. IntUnaryOperator: accepts a parameter of type int and the return value of type int.
  24. LongBinaryOperator: takes two arguments of type long and returns a value of type long.
  25. LongConsumer: Takes an input parameter of type long, with no return value.
  26. LongFunction

    : takes an input parameter of type long and returns a result.
  27. LongPredicate: R takes a long input parameter and returns a Boolean type result.
  28. LongSupplier: Returns a value of type long with no arguments.
  29. LongToDoubleFunction: Takes a long input and returns a double result.
  30. LongToIntFunction: takes a long input and returns an int result.
  31. LongUnaryOperator: accepts an argument of type long and returns a value of type long.
  32. ObjDoubleConsumer

    : takes an input parameter of type Object and type double, with no return value.
  33. ObjIntConsumer

    : accepts an input parameter of type object and type int, with no return value.
  34. ObjLongConsumer

    : takes an input parameter of type Object and type long, with no return value.
  35. Predicate

    : Takes an input parameter and returns a Boolean result.
  36. Supplier

    : No argument, returns a result.
  37. ToDoubleBiFunction

    : Takes two input arguments and returns a double result
    ,u>
  38. ToDoubleFunction

    : takes an input argument and returns a double result
  39. ToIntBiFunction

    : Takes two input arguments and returns an int.
    ,u>
  40. ToIntFunction

    : takes an input argument and returns an int result.
  41. ToLongBiFunction

    : Takes two input arguments and returns a result of type long.
    ,u>
  42. ToLongFunction

    : takes an input argument and returns a result of type long.
  43. UnaryOperator

    : accepts an argument of type T and returns a value of type T.