1, lambda expressions Introduction
Java 8 biggest change is the introduction of Lambda (Lambda is the Greek letter λ English name) expression - a compact way transfer behavior.
lambda write good code that can greatly reduce redundancy, while readability is better than lengthy inner classes, anonymous classes.
- Create a thread
- Sequence
2, lambda expression syntax
2.1, the general syntax for lambda expressions
(Type1 param1, Type2 param2, ..., TypeN paramN) -> {
statment1;
statment2;
//.............
return statmentM;
}
This is the lambda expression syntax fully , several followed by its syntax simplified .
2.2, a single parameter syntax
param1 -> {
statment1;
statment2;
//.............
return statmentM;
}
When the number of only one parameter lambda expressions, parentheses may be omitted
For example: to convert the string list as lowercase
List<String> proNames = Arrays.asList(new String[]{"Ni","Hao","Lambda"});
List<String> lowercaseNames1 = proNames.stream().map(name -> {return name.toLowerCase();}).collect(Collectors.toList());
2.3, a single written statement
param1 -> statment
When the lambda expression contains only one statement, you can omit the semicolon braces, return and the end of the sentence
For example: to convert the string list as lowercase
List<String> proNames = Arrays.asList(new String[]{"Ni","Hao","Lambda"});
List<String> lowercaseNames2 = proNames.stream().map(name -> name.toLowerCase()).collect(Collectors.toList());
3, variable lambda expressions can be used
First example:
// add a prefix string for a string list
String waibu = "the lambda:";
List <String> proStrs = Arrays.asList (new new String [] { "of Ni", "Hao", "the Lambda"});
List <String> execStrs = proStrs.stream () Map (chuandi -> {.
Long zidingyi = System.currentTimeMillis ();
return waibu chuandi + + "-----:" + zidingyi;
}) the collect (Collectors.. toList ());
execStrs.forEach (the System.out :: the println);
Output:
lambda: Me: 1474622341604 private rooms
lambda: Hao: 1474622341604 private rooms
lambda: Lambda: 1474622341604 private rooms
Variable waibu: external variables
Variable chuandi: variable transmission
Variable zidingyi: internal custom variables
lambda expression can access to the variable it passes , access to their own internal definitions of variables , but also can access its external variables .
However, lambda expressions to access external variables that have a very important limitation: Variable immutable (immutable reference only, rather than a true immutable).
When the modified internal expression waibu = waibu + ""; when, IDE will prompt you:
Local variable waibu defined in an enclosing scope must be final or effectively final
Will compile-time error. Because the variable is referenced waibu lambda expressions, so the compiler would implicitly put it as a final handled .
Previous Java anonymous inner classes when accessing external variables, external variables must be modified by the final. Now java8 this restriction is optimized, you can not use the final modification of the display, but the compiler implicitly handled as final.
4, lambda expressions of this concept
In the lambda, the this is not the point to the SAM objects produced by lambda expressions, but declare its external objects.
E.g:
public class WhatThis {
public void whatThis(){
//转全小写
List<String> proStrs = Arrays.asList(new String[]{"Ni","Hao","Lambda"});
List<String> execStrs = proStrs.stream().map(str -> {
System.out.println(this.getClass().getName());
return str.toLowerCase();
}).collect(Collectors.toList());
execStrs.forEach(System.out::println);
}
public static void main(String[] args) {
WhatThis wt = new WhatThis();
wt.whatThis();
}
}
Output:
com.wzg.test.WhatThis
com.wzg.test.WhatThis
com.wzg.test.WhatThis
ni
hao
lambda
5, reference method
Methods cited by the method name to point to a method .
The method reference can be made more compact and simple construction of the language, to reduce redundant code.
Method references a pair of colons ::
Here, we define four classes method Car cited as an example in Java distinguish four different methods.
package com.runoob.main;
@FunctionalInterface
public interface Supplier<T> {
T get();
}
class Car {
/ Supplier jdk1.8 interface is used here together and lamda
public static Car create(final Supplier<Car> supplier) {
return supplier.get();
}
public static void collide(final Car car) {
System.out.println("Collided " + car.toString());
}
public void follow(final Car another) {
System.out.println("Following the " + another.toString());
}
public void repair() {
System.out.println("Repaired " + this.toString());
}
}
-
5.1, constructor references:
-
The syntax is Class :: new new , or more generally Class <T> :: new examples are as follows:
-
final Car car = Car.create( Car::new ); final List< Car > cars = Arrays.asList( car );
-
5.2, static method references:
-
Its syntax is Class :: static_method , examples are as follows:
cars.forEach( Car::collide );
-
5.3, any object class-specific method references:
-
Its syntax is Class :: method examples are as follows:
cars.forEach( Car::repair );
-
5.4, a reference to a specific object method:
-
Its syntax is the instance :: method examples are as follows:
final Car police = Car.create( Car::new );
-
cars.forEach( police::follow );
5.5, refer to an instance method
Enter the following code in Java8Tester.java file:
Java8Tester.java file
import java.util.List; java.util.ArrayList import;
public class Java8Tester {
public static void main(String args[]){
List names = new ArrayList();
names.add("Google");
names.add("Runoob");
names.add("Taobao");
names.add("Baidu");
names.add("Sina");
names.forEach(System.out::println);
}
}
Example we will System.out :: println method as a static method to reference.
The implementation of the above script, output is:
$ javac Java8Tester.java
$ java Java8Tester
Google
Runoob
Taobao
Baidu
Sina