1. 簡介
-
什么是 Lambda
Lambda 表達式是 JDK8 的一個新特性,可以取代大部分的匿名內(nèi)部類,寫出更優(yōu)雅的 Java 代碼,尤其在集合的遍歷和其他集合操作中,可以極大地優(yōu)化代碼結(jié)構(gòu)。
同時,JDK 也提供了大量的內(nèi)置函數(shù)式接口供我們使用,使得 Lambda 表達式的運用更加方便、高效。 基本語法
語法形式為 () -> {},其中 () 用來描述參數(shù)列表,{} 用來描述方法體,-> 為 lambda 運算符 ,讀作 (goes to)。
?
2. 應用
能用 Lambda 表達式來表示的類型,必須是一個函數(shù)式接口。
-
什么是函數(shù)式接口(Functional Interface)
函數(shù)接口是只有一個抽象方法的接口,用作 Lambda 表達式的類型。
使用@FunctionalInterface 注解修飾的類,編譯器會檢測該類是否只有一個抽象方法或接口,否則,會報錯。 -
java8 自帶的常用函數(shù)式接口
image.png java.util.function包下的幾個重要的類
單個入?yún)⒒驘o入?yún)?/p>
| 方法 | 解釋 |
|---|---|
| Consumer void accept(T t) | 有入?yún)?,無返回值 |
| Function R apply(T t) | 有入?yún)?,有返回?/td> |
| Predicate boolean test(T t) | 有入?yún)?,返?boolean 值 |
| Supplier T get() | 沒有入?yún)?,有返回?/td> |
多個入?yún)?/p>
| 方法 | 解釋 |
|---|---|
| BiConsumer void accept(T t, U u) | 兩個入?yún)?,無返回值 |
| BiFunction R apply(T t, U u) | 兩個入?yún)?,有返回?/td> |
| BiPredicate boolean test(T t, U u) | 兩個入?yún)?,返?boolean 值 |
- 方法引用
方法引用是一種特殊的lambda,當方法體中只有一個方法調(diào)用時,就可以用[類名::方法名]來簡化。
eg:
Function<String, Integer> toLength = s -> s.length();
Function<String, Integer> toLength = String::length;
Function<User, String> getName = user -> user.getName();
Function<String, Integer> toLength = User::getName;
Consumer<String> printer = s -> System.out.println(s);
Consumer<String> printer = System.out::println;
// 構(gòu)造方法的簡化
Supplier<List<String>> newListOfStrings = () -> new ArrayList<>();
Supplier<List<String>> newListOfStrings = ArrayList::new;
?
3. 惰性求值和Java8的一些常用的流API
什么是惰性求值
惰性求值即Java8的Stream操作。惰性求值操作的結(jié)果也是Stream,惰性求值可以像建造者模式一樣鏈式使用,最后再使用及早求值得到最終結(jié)果。
常用的流API
- collect(Collectors.toList())
及早求值函數(shù): 將流轉(zhuǎn)換為List,Set對應toSet(),Map對應toMap()等。
eg:
public class TestCase {
public static void main(String[] args) {
List<Student> studentList = Stream.of(new Student("路飛", 22, 175),
new Student("紅發(fā)", 40, 180),
new Student("白胡子", 50, 185)).collect(Collectors.toList());
System.out.println(studentList);
}
}
輸出結(jié)果:
[Student{name='路飛', age=22, stature=175, specialities=null},
Student{name='紅發(fā)', age=40, stature=180, specialities=null},
Student{name='白胡子', age=50, stature=185, specialities=null}]
- filter
惰性求值函數(shù): 起過濾篩選的作用,內(nèi)部就是Predicate接口。
eg:
public class TestCase {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飛", 22, 175));
students.add(new Student("紅發(fā)", 40, 180));
students.add(new Student("白胡子", 50, 185));
List<Student> list = students.stream()
.filter(stu -> stu.getStature() < 180)
.collect(Collectors.toList());
System.out.println(list);
}
}
輸出結(jié)果:
[Student{name='路飛', age=22, stature=175, specialities=null}]
- map
惰性求值函數(shù): 映射函數(shù),實現(xiàn)轉(zhuǎn)換功能,內(nèi)部就是Function接口。
eg:
public class MapDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飛", 22, 175));
students.add(new Student("紅發(fā)", 40, 180));
students.add(new Student("白胡子", 50, 185));
List<String> names = students.stream().map(Student::getName)
.collect(Collectors.toList());
System.out.println(names);
}
}
輸出結(jié)果:
[路飛, 紅發(fā), 白胡子]
- flatMap
惰性求值函數(shù): 將多個Stream合并為一個Stream。
eg:
public class FlatMapDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飛", 22, 175));
students.add(new Student("紅發(fā)", 40, 180));
students.add(new Student("白胡子", 50, 185));
List<Student> studentList = Stream.of(students,
Arrays.asList(new Student("艾斯", 25, 183),
new Student("雷利", 48, 176)))
.flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(studentList);
}
}
輸出結(jié)果:
[Student{name='路飛', age=22, height=175.0}
, Student{name='紅發(fā)', age=40, height=180.0}
, Student{name='白胡子', age=50, height=185.0}
, Student{name='艾斯', age=25, height=183.0}
, Student{name='雷利', age=48, height=176.0}
]
- max和min
及早求值函數(shù): 在集合中求最大或最小值。
eg:
public class MaxMinDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飛", 22, 175));
students.add(new Student("紅發(fā)", 40, 180));
students.add(new Student("白胡子", 50, 185));
Optional<Student> max = students.stream()
.max(Comparator.comparing(Student::getAge));
Optional<Student> min = students.stream()
.min(Comparator.comparing(Student::getAge));
//判斷是否有值
max.ifPresent(System.out::println);
min.ifPresent(System.out::println);
}
}
輸出結(jié)果:
Student{name='白胡子', age=50, height=185.0}
Student{name='路飛', age=22, height=175.0}
- count
及早求值函數(shù): 統(tǒng)計功能,一般都是結(jié)合filter使用,先篩選出我們需要的再統(tǒng)計即可。
eg:
public class CountDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飛", 22, 175));
students.add(new Student("紅發(fā)", 40, 180));
students.add(new Student("白胡子", 50, 185));
long count = students.stream().filter(s1 -> s1.getAge() < 45).count();
System.out.println("年齡小于45歲的人數(shù)是:" + count);
}
}
輸出結(jié)果:
年齡小于45歲的人數(shù)是:2
- reduce
及早求值函數(shù): 可以實現(xiàn)從一組值中生成一個值。
eg:
public class ReduceSumDemo {
public static void main(String[] args) {
Integer reduce = Stream.of(1, 2, 3, 4, 5).reduce(0, Integer::sum);
System.out.println(reduce);
}
}
輸出結(jié)果:
15
