Lambda expressions are an important new feature in Java SE 8. Lambda expressions allow you to replace a functional interface with an expression. The lambda expression, like a method, provides a normal argument list and a body that uses these parameters (body, which can be an expression or a block of code).
The lambda expression also enhances the collection library. Java SE 8 adds 2 packages for bulk operation of collection data: The Java.util.function package and the Java.util.stream package. Stream is like an iterator (iterator), but many additional features are attached. In general, lambda expressions and streams are the biggest changes since the Java language added generics (generics) and annotations (annotation).
The syntax of a lambda expression
Basic syntax:
(parameters), expression
Or
(parameters), {statements;}
Here is a simple example of a Java lambda expression:
// 1. No parameters are required, the return value is 5(), 5 /// 2. Receives a parameter (number type), returns twice times its value x, 2 * x // 3. Accept 2 parameters (numbers) and return their difference (x, y)- computes // 4. Receive 2 int integers, return their and (intint y), x + y // 5. Accepts a string object and prints it in the console without returning any values ( Looks like a return void) (String s), System.out.print (s)
Basic Lambda Example
Loop a list
string[] ATP = {"Rafael Nadal", "Novak Djokovic", "Stanislas Wawrinka", "David Ferrer", "Roger Federer", "Andy Murray", "Tomas Berdych", "Juan Martin Del Potro"};
Convert an array into a collection list<String> players =arrays.aslist (ATP); //the previous loop mode for(String player:players) {System.out.print (player+ "; "); } //using lambda expressions and function actions (functional operation)Players.foreach (player), System.out.print (player + ";"))); //using the double colon operator in Java 8 (double colon operator)Players.foreach (System.out::p rintln);
Implementing anonymous internal classes
//1.1 using anonymous internal classesNewThread (NewRunnable () {@Override Public voidrun () {System.out.println ("Hello World!"); }}). Start (); //1.2 using lambda expressionNewThread (()-System.out.println ("Hello world!")) . Start (); //2.1 Using anonymous internal classesRunnable Race1 =NewRunnable () {@Override Public voidrun () {System.out.println ("Hello World!"); } }; //2.2 using lambda expressionRunnable Race2 = (), System.out.println ("Hello world!")); //call the Run method directly (no new thread is opened!) Race1.run (); Race2.run ();
Sort Collection
String[] players = {"Rafael Nadal", "Novak Djokovic",
"Stanislas Wawrinka", "David Ferrer", "Roger Federer", "Andy Murray.", "Tomas Berdych", "Juan Martin Del Potro", "Richard Gasquet", "John Isner."}; //1.1 using anonymous inner classes to sort by name playersArrays.sort (Players,NewComparator<string>() {@Override Public intCompare (string s1, string s2) {return(S1.compareto (S2)); } }); //1.2 using lambda expression to sort playersComparator<string> sortbyname = (string s1, string s2)(S1.compareto (S2)); Arrays.sort (players, sortbyname); //1.3 can also take the following form:Arrays.sort (Players, (string s1, string s2)(S1.compareto (S2)));
1.4 Sorting using double colons
Arrays.sort (Players, (String::compareto));
Java8 lambda expression