Java9 all out, I began to touch the new characteristics of java8, a little disjointed ah.
Lambda is an anonymous function that can be understood as a piece of code that can be passed, passing code like data, and here's a small example.
Public classemployee{PrivateString name; Private intAge ; Private intsalary;//Getter and Setter}//----------------------------------------------- Public InterfaceMyinter {Boolean filter (Employee list);}//----------------------------------------------- Public classMyinterimplImplementsMyinter {@Override PublicBoolean Filter (Employee list) {returnList.getage () >35; }}//----------------------------------------------- PackageLambdap;Importorg.junit.Test;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List; Public classLambdademo { Public Static FinalList<employee> employees =Arrays.aslist (NewEmployee ("Zhang San", 19, 2000), NewEmployee ("Zhang Si", 33, 3000), NewEmployee ("Zhang Five", 38, 4000), NewEmployee ("Zhang Six", 41, 2500), NewEmployee ("Zhang Seven", 42, 5500) ); //Requirement: Employee information for employees in the current company is more than 35 cold//Traditional Methods PublicList<employee>GetEmployees () {List<Employee> list =NewArraylist<>(); for(Employee employee:employees) {if(Employee.getage () > 35) {List.add (employee); } } returnlist; } Public voidtest1 () {List<Employee> employees = This. GetEmployees (); for(Employee e:employees) {System.out.println (e); } } //Method 2 PublicList<employee> Filteremployee (list<employee>list,myinter Inter) {List<Employee> NewList =NewArraylist<>(); for(Employee employee:list) {if(Employee.getage () >35) {Newlist.add (employee); } } returnNewList; } Public voidtest2 () {List<Employee> employees = This. Filteremployee (Lambdademo.employees,NewMyinterimpl ()); for(Employee employee:employees) {SYSTEM.OUT.PRINTLN (employee); } } //Lambda@Test Public voidtest3 () {List<Employee> employees = This. Filteremployee (Lambdademo.employees, (e), E.getage () > 35); for(Employee employee:employees) {SYSTEM.OUT.PRINTLN (employee); } System.out.println ("Lambda ..."); } //method Three: If none of the above exist, the Stream API is also newly updated in Java8@Test Public voidtest4 () {Employees.stream (). Filter ((e)->e.getage () >35). ForEach (System.out::p rintln); }}
Above you can see the convenience of lambda, the following start my own study notes
Importorg.junit.Test;ImportJava.util.Comparator;ImportJava.util.function.Consumer; Public classLamBdaDemo2 {/** * Basic syntax for lambda: The new "--" operator, called the arrow operator or the lambda operator, is introduced in Java8, and he splits the lambda expression into two parts * 1. Left: Parameter list for lambda * 2. Right: The function to be performed by lambda, the parameter list on the left side of the lambda body * arrow operator, and the method to be implemented on the right. * JDK 1.7 before the internal class reference sibling external variables must be final, now do not manually add, default, but still in the inner class can not manipulate external variables, lambda also applies. * Syntax Format one: no parameters, no return value test1 * (), System.out.println ("Hello"); * Syntax Format two: There is a parameter, no return value Test2 * (e), {implementation}; * Syntax format three: There are more than two parameters, and the lambda body has more than one statement, there is a return value. TEST3 * (x, y)->{implementation}; * Syntax format four: LAMBDA expression parameter list type can omit to write, if write must write all, the JVM can infer the parameter type by itself. * Comparator<integer> Comparator2 = (Integer x,integer y), Integer.compare (x, y); * * Around a bracket province: that is, lambda weight on both sides if there is only one argument or a statement, the parentheses can be omitted to write. * Left inference Type province: The JVM infers it by itself. * Lambda expressions require "functional interface support" * Functional interface: An interface with only one abstract method in an interface is called a functional interface * Click on the interface name with CTRL + Left click to see if the system-provided interface is a functional interface * The functional interfaces provided by the system are annotated with @FunctionalInterface annotations * This annotation can check if it is a functional interface */ Public voidtest1 () {Runnable runnable1=NewRunnable () {@Override Public voidrun () {System.out.println ("Hello"); } }; Runnable1.run (); System.out.println ("*********************"); Runnable Runnable= ()-System.out.println ("Hello Lambda"); Runnable.run (); } Public voidtest2 () {//Consumer definition @FunctionalInterface//Public interface Consumer<t> {//method in Consumer void accept (T t);Consumer<string> Consumer = (e), System.out.println ("Lambda" +e); Consumer.accept ("Hello"); //in the case of only one argument, the parentheses can be omitted.consumer<string> con = e-System.out.println ("Lambda" +e); Con.accept ("Hello"); } Public voidtest3 () {Comparator<Integer> comparator = (x, y){System.out.println ("Compare X Y"); returninteger.compare (x, y); }; System.out.println (Comparator.compare (The)); System.out.println ("**************************"); //If there is only one statement after the curly brace can be omitted to writeComparator<integer> Comparator2 = (x, y)integer.compare (x, y); intCompare = Comparator2.compare (2, 1); System.out.println (Compare); }}
Here's a small feature: any operation on two numbers
@FunctionalInterface Public Interface MyInter2 { integer getValue (integer x);}
java--8--new Features--lambda