1.8 overview
- Java8 (also known as JDK1.8) is a major version of Java language development. Oracle released Java 8.0 on March 18, 2014
- Lambda expressions are supported
- Functional interface
- New Stream API
- New date API
2.Lambda expression
- Lambda expression: special anonymous inner class, with more concise syntax.
- Lambda expressions allow functions to be passed as parameters of a method (functions are passed as method parameters) and code to be passed as data.
example:
package Test; import java.util.Comparator; import java.util.TreeSet; /** * Lambda Expression use * @author DELL * */ public class Demo1 { public static void main(String[] args) { //Anonymous Inner Class Runnable runnable=new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println("The child thread executes..."); } }; //Lambda expression Runnable runnable2=()->System.out.println("Sub thread 2 executes..."); new Thread(runnable).start(); new Thread(runnable2).start(); new Thread(()->System.out.println("Sub thread 3 executes...")).start(); / //Anonymous Inner Class Comparator<String> com=new Comparator<String>() { @Override public int compare(String o1, String o2) { // TODO Auto-generated method stub return o1.length()-o2.length(); } }; //Lambda expression Comparator<String> com2=(String o1, String o2)-> { return o1.length()-o2.length(); }; Comparator<String> com3=( o1, o2)->o1.length()-o2.length(); TreeSet<String> treeSet=new TreeSet<>(com3); } }
- Basic grammar
< functional interface > < variable name > = (parameter 1, parameter 2...) - >{
//Method body
} - Lambda introduces a new operator: - > (arrow operator), - > divides the expression into two parts
- On the left: (parameter 1, parameter 2...) indicates the parameter list
- Right: {} inside is the method body
- matters needing attention
- The data type of the formal parameter list is automatically inferred
- If the formal parameter list is empty, just keep ()
- If there is only one formal parameter, () can be omitted and only the name of the parameter is required
- If the execution statement has only one sentence and no return value, {} can be omitted. If there is a return value, if you want to omit {}, you must omit return at the same time, and the execution statement must also have only one sentence.
- Lambda does not generate a separate internal class file
3. Functional interface
- If an interface has only one abstract method, it is called a functional interface. A functional interface can use a Lambda expression, which will be matched to the abstract method.
- @The functional interface annotation detects whether the interface conforms to the functional interface.
example:
package Test; /** * Functional interface * @author DELL * */ @FunctionalInterface public interface Usb { void service(); }
package Test; public class Demo2 { public static void main(String[] args) { //Anonymous Inner Class Usb mouse=new Usb() { @Override public void service() { System.out.println("The mouse starts working...."); } }; Usb fan=()->System.out.println("The fan starts to work...."); run(mouse); run(fan); } public static void run(Usb usb) { usb.service(); } }
Common functional interfaces
Functional interface | Parameter type | Return type | explain |
---|---|---|---|
Consumer consumer interface | T | void | void accept(T t); Apply an action to an object of type T |
Supplier supply interface | nothing | T | T get(); Returns an object of type T |
Function < T, R > functional interface | T | R | R apply(T t);d applies an operation to an object of type T and returns an object of type R. |
Predicate assertion interface | T | boolean | boolean test(T t); Determines whether the object of type T satisfies the condition and returns the boolean type. |
Comsumer example:
package Test; import java.util.function.Consumer; public class Demo3 { public static void main(String[] args) { //Anonymous Inner Class // Consumer<Double> consumer=new Consumer<Double>() { // @Override // public void accept(Double t) { // System.out.println("dinner consumption:" + t); // } // }; //Lambda expression // Consumer<Double> consumer=t->System. out. Println ("dinner consumption:" + t); happy(t->System.out.println("Dinner consumption:"+t), 1000); happy(t->System.out.println("Singing consumption:"+t), 2000); happy(t->System.out.println("Foot therapy consumption:"+t), 3000); } //Consumer consumer interface public static void happy(Consumer<Double> consumer,double money) { consumer.accept(money); } }
Suuplier example:
} //Supplier supply interface public static int[] getNums(Supplier<Integer> supplier,int count ) { int[] arr=new int[count]; for (int i = 0; i < count; i++) { arr[i]=supplier.get(); } return arr; }
int[] arr=getNums(()->new Random().nextInt(100),5); System.out.println(Arrays.toString(arr));
Function example
//Function interface public static String handlerString(Function<String,String> function,String str) { return function.apply(str); }
String result = handlerString(s->s.toUpperCase(),"hello"); System.out.println(result); String result1 = handlerString(s->s.trim()," zhangshan "); System.out.println(result1);
Predicate assertion:
//Predicate assertion interface public static List<String> filterNames(Predicate<String> predicate,List<String> list){ List<String> resultList=new ArrayList<String>(); for (String string : list) { if(predicate.test(string)) { resultList.add(string); } } return resultList; }
List<String> names=new ArrayList<>(); names.add("zhangsan"); names.add("zhangwuji"); names.add("lisi"); names.add("wangwu"); names.add("zhaoliu"); List<String> result = filterNames(s->s.startsWith("zhang"),names); System.out.println(result.toString()); List<String> result2 = filterNames(s->s.length()>5,names); System.out.println(result2);
4. Method reference
- Method reference is a short form of Lambda expression. If only a specific existing method is called in the method body of Lambda expression, method reference can be used.
- Common forms
- Object:: instance method
- Class:: static method
- Class:: instance method
- Class:: new
example:
package Test; import java.util.Comparator; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; public class Demo4 { public static void main(String[] args) { //1. Object: instance method Consumer<String> consumer=s->System.out.println(s); consumer.accept("hello"); Consumer<String> consumer2=System.out::println; consumer2.accept("world"); //Class 2: static methods Comparator<Integer> com=(o1,o2)->Integer.compare(o1,o2); Comparator<Integer> com2=Integer::compare; //Class 3: instance method Function<Employee,String> function=e->e.getName(); Function<Employee,String> function2=Employee::getName; System.out.println(function2.apply(new Employee("Xiao Ming",50000))); //4. Class:: new Supplier<Employee> supplier=()->new Employee(); Supplier<Employee> supplier2=Employee::new; Employee employee = supplier2.get(); System.out.println(employee.toString()); } }