Member-only story

Lambda Expression Notes

Beck Moulton
3 min readMay 10, 2023

--

Lambda expression is a new syntax form after the beginning of JDK8. It is an anonymous function method that can replace most of the anonymous inner classes and write more elegant Java code, especially in the traversal of collections and other operations, which can greatly Optimize the code structure, and its essence belongs to the concept of function programming.

Supplier interface

Supplier is a supply interface, where the get method is used to return a value; Supplier also has many variants, such as IntSupplier , LongSupplier and BooleanSupplier .

  • The java.util.function. Supplier < T > interface contains only one method without arguments: T get () . Used to get object data of the type specified by a generic parameter.
  • The Supplier < T > interface is called a production interface. It specifies what type the generic type of the interface is, so the get method in the interface will produce what type of data.
  • Example: Converting an object to a string
private static String toStr(Object object) {
Supplier<String> supplier = () -> object.toString();
return supplier.get();
}

Consumer interface

Consumer is a consumer interface that receives an input parameter and has no return operation, that is, when a…

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet