Familiar with the new features of JDK8, "Lambda expressions and functional interfaces"

Hello, hello, I am Dabai (●—●)


lambda expression

Lambda expressions can be seen as a concise way of writing anonymous inner classes.
Grammatically, Lambda expressions consist of three parts:
parameter list, arrow, and body, such as :
(parameters)->expression
or
(parameters)->{statements}

Functional interface

Lambda expressions used in the function interface, so-called functional interfaces, only defines an abstract method interface (Interface)
if there is a default interface method does not affect
comment @FunctionalInterface can help us to prevent errors in the design of functional interfaces
we Commonly used Runnable and Callable are functional interfaces.
JDK8 has added several functional interfaces:

  • Predicate<T>:
    Contains the test method, receives the generic T, and returns boolean, which can be regarded as an assertion (check) interface
  • Consumer<T>:
    Contains the accept method, receives the generic T, no return, can be regarded as a data consumption interface
  • Function<T, R>:
    Contains the apply method, receives the generic T, and returns R, which can be regarded as a mapping conversion interface

ps: The new function interfaces are more than these, but simple usage is introduced. improve together

Dabai (●—●) accompany you to make progress together!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42292697/article/details/112671414