First, what is the Lambada expression
First look at the following section of code!
Package cn.org.kingdom.jdk8; @FunctionalInterfaceinterface myinterface {void Test (); String toString ();} public class Test {public void Test (MyInterface inter) {System.out.println ("*************************"); Inter.test (); System.out.println ("*************************");} public static void Main (string[] args) {Test t = new Test (); T.test (()->{system.out.println ("Lambada expression Implementation");}); MyInterface my = ()->{}; System.out.println (my); System.out.println (My.getclass ()); System.out.println (My.getclass (). Getsuperclass ()); System.out.println (My.getclass (). getinterfaces () [0]);}}
Printing results are as follows
*************************
Lambada Expression Implementation
*************************
cn.org.kingdom.jdk8.test$ $Lambda $2/[email protected]
Class cn.org.kingdom.jdk8.test$ $Lambda $2/9195351
Class Java.lang.Object
Interface Cn.org.kingdom.jdk8.MyInterface
Second, look at the previous example again
list<integer> list = Arrays.aslist (1, 2, 3, 4, 5, 6, 7, 8); List.foreach (new consumer<integer> () {@Overridepubl IC void Accept (Integer t) {System.out.println (t);}});
For the Foreach method of list, let's take a look at where the definition of this method
Public interface Iterable<t> { iterator<t> Iterator ();
default void ForEach (CONSUMER<? Super T> action) { objects.requirenonnull (action); for (t t:this) { action.accept (t); } } Default Spliterator<t> spliterator () { return spliterators.spliteratorunknownsize (iterator (), 0);} }
For the above code, we can see in JDK1. 8, the interface added the default method (to ensure that the jdk1.8 new features, but also to ensure that the JDK8 and the old version of compatibility), the following we focus on this foreach method
/**
* Performs the given action for each element of the {@code iterable}
* Until all elements has been processed or the action throws an
* Exception. Unless otherwise specified by the implementing class,
* Actions is performed in the Order of iteration (if an iteration order
* is specified). Exceptions thrown by the action is relayed to the
* Caller.
For each element of the interface, execute the given parameter until all the elements have been processed, or the operation throws an exception, and if not specified by the implementation class, the action is executed in the order of the iteration
Whether the exception is thrown depends on the caller
*/
Next, look at the definition of the consumer interface
Package Java.util.function;import java.util.objects;/**
* Represents an operation, accepts a single input argument and returns no * result. Unlike most other functional interfaces, {@code Consumer} are expected * to operate via side-effects.
* Represents an operation, this operation takes a single input parameter, and does not produce a result, and most of the functions are different, it may cause side effects * * <p>this is a <a href= "package-summary.html" > Functional Interface</a> * Whose functional method is {@link #accept (Object)}. * This functional interface function method is accept * @param <T> the type of the input to the operation * * @since 1.8 */@FunctionalInterfacepubli C interface Consumer<t> {/** * performs this operation on the given argument. * @param t the input argument */void accept (T t); /** * Returns A composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code afte R} operation. If performing either * operation throws an exception, it's relayed to the caller of the * composed operation. If performing this operation throws a exception, * the {@code after} operation would not be performed. * * @param after the operation-perform after this operation * @return a composed {@code Consumer} that performs In sequence this * OperatiOn followed by the {@code through} operation * @throws nullpointerexception if {@code after} is null */default C Onsumer<t> andthen (consumer<? Super T> after) {objects.requirenonnull (after); Return (T T), {accept (T); after.accept (t);}; }}
Next, use a functional interface to re-implement the case
list<integer> list = Arrays.aslist (1, 2, 3, 4, 5, 6, 7, 8); List.foreach (I->{system.out.println (i);});
Lambda expressions add missing functional programming features to Java, allowing us to treat functions as a citizen
The type of lambda expression is a function in the language where the function is a class-one citizen, but in Java, the lambda expression is an object and they must be attached to a particular type of object-the functional interface
can also be implemented in the following ways
list<integer> list = Arrays.aslist (1, 2, 3, 4, 5, 6, 7, 8);//By method reference (methods references) List.foreach (system.out::p Rin T);
All right, write it down here today! About the basic use of lambda expressions, I believe you should have a basic understanding of
JAVA8 Learning Notes (ii)-functional interfaces and method references