流使程序猿可以在抽象層上對集合進(jìn)行操作。
從外部迭代到內(nèi)部迭代
什么是外部迭代和內(nèi)部迭代呢?
個(gè)人認(rèn)為,外和內(nèi)是相對集合代碼而言。
如果迭代的業(yè)務(wù)執(zhí)行在應(yīng)用代碼中,稱之為外部迭代。
反之,迭代的業(yè)務(wù)執(zhí)行在集合代碼中,稱為內(nèi)部迭代(函數(shù)式編程)。
語言描述可能有點(diǎn)抽象,下面看實(shí)例。
1. 外部迭代
調(diào)用itrator方法,產(chǎn)生一個(gè)新的Iterator對象,進(jìn)而控制整個(gè)迭代過程。
for (Student student:list){
if (student.getAge()>18){
result++;
}
}
我們都知道,for其實(shí)底層使用的迭代器:
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()){
Student student = iterator.next();
if (student.getAge()>18){
result++;
}
}
上面的迭代方法就是外部迭代。
外部迭代缺點(diǎn):
- 很難抽象出復(fù)雜操作
- 本質(zhì)上講是串行化操作。
2. 內(nèi)部迭代
返回內(nèi)部迭代中的響應(yīng)接口:Stream
long count = list.stream().filter(student -> student.getAge() > 18).count();
整個(gè)過程被分解為:過濾和計(jì)數(shù)。
要注意:返回的Stream對象不是一個(gè)新集合,而是創(chuàng)建新集合的配方。
2.1 惰性求值和及早求值
像filter這樣值描述Stream,最終不產(chǎn)生新集合的方法叫做惰性求值。
像count這樣最終會(huì)從Stream產(chǎn)生值的方法叫做及早求值。
判斷一個(gè)操作是惰性操作還是及早求值,只需看它的返回值。如果返回值是Stream,那么是惰性求值;如果返回值是另一個(gè)值或者為空,那就是及早求值。這些操作的理想方式就是形成一個(gè)惰性求值的鏈,最后用一個(gè)及早求值的操作返回想要的結(jié)果。
整個(gè)過程跟建造者模式很像,使用一系列的操作后最后調(diào)用build方法才返回真正想要的對象。設(shè)計(jì)模式快速學(xué)習(xí)(四)建造者模式
那這個(gè)過程有什么好處呢:可以在集合類上級聯(lián)多種操作,但迭代只需要進(jìn)行一次。
3. 常用操作
3.1 collect(toList()) 及早求值
collect(toList())方法由Stream里的值生成一個(gè)列表,是一個(gè)及早求值操作。
List<String> collect = Stream.of("a", "b", "c").collect(Collectors.toList());
Stream.of("a", "b", "c")首先由列表生成一個(gè)Stream對象,然后collect(Collectors.toList())生成List對象。
3.2 map
map可以將一種類型的值轉(zhuǎn)換成另一種類型。
List<String> streamMap = Stream.of("a", "b", "c").map(String -> String.toUpperCase()).collect(Collectors.toList());
map(String -> String.toUpperCase())將返回所有字母的大寫字母的Stream對象,collect(Collectors.toList())返回List。
3.3 filter過濾器
遍歷并檢查其中的元素時(shí),可用filter
List<String> collect1 = Stream.of("a", "ab", "abc")
.filter(value -> value.contains("b"))
.collect(Collectors.toList());
3.4 flatMap
如果有一個(gè)包含了多個(gè)集合的對象希望得到所有數(shù)字的集合,我們可以用flatMap
List<Integer> collect2 = Stream.of(asList(1, 2), asList(3, 4))
.flatMap(Collection::stream)
.collect(Collectors.toList());
Stream.of(asList(1, 2), asList(3, 4))將每個(gè)集合轉(zhuǎn)換成Stream對象,然后.flatMap處理成新的Stream對象。
3.5 max和min
看名字就知道,最大值和最小值。
Student student1 = list.stream()
.min(Comparator.comparing(student -> student.getAge()))
.get();
java8提供了一個(gè)Comparator靜態(tài)方法,可以借助它實(shí)現(xiàn)一個(gè)方便的比較器。其中Comparator.comparing(student -> student.getAge()可以換成Comparator.comparing(Student::getAge)成為更純粹的lambda。max同理。
3.6 reduce
reduce操作可以實(shí)現(xiàn)從一組值中生成一個(gè)值,在上述例子中用到的count、min、max方法事實(shí)上都是reduce操作。
Integer reduce = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
System.out.println(reduce);
6
上面的例子使用reduce求和,0表示起點(diǎn),acc表示累加器,保存著當(dāng)前累加結(jié)果(每一步都將stream中的元素累加至acc),element是當(dāng)前元素。
4. 操作整合
- collect(toList())方法由Stream里的值生成一個(gè)列表
- map可以將一種類型的值轉(zhuǎn)換成另一種類型。
- 遍歷并檢查其中的元素時(shí),可用filter
- 如果有一個(gè)包含了多個(gè)集合的對象希望得到所有數(shù)字的集合,我們可以用flatMap
- max和min
- reduce(不常用)
5. 鏈?zhǔn)讲僮鲗?shí)戰(zhàn)
List<Student> students = new ArrayList<>();
students.add(new Student("Fant.J",18));
students.add(new Student("小明",19));
students.add(new Student("小王",20));
students.add(new Student("小李",22));
List<Class> classList = new ArrayList<>();
classList.add(new Class(students,"1601"));
classList.add(new Class(students,"1602"));
static class Student{
private String name;
private Integer age;
getter and setter ...and construct ....
}
static class Class{
private List<Student> students;
private String className;
getter and setter ...and construct ....
}
這是我們的數(shù)據(jù)和關(guān)系--班級和學(xué)生,現(xiàn)在我想要找名字以小開頭的學(xué)生,用stream鏈?zhǔn)讲僮鳎?/p>
List<String> list= students.stream()
.filter(student -> student.getAge() > 18)
.map(Student::getName)
.collect(Collectors.toList());
[小明, 小王, 小李]
這是一個(gè)簡單的students對象的Stream的鏈?zhǔn)讲僮鲗?shí)現(xiàn),那如果我想要在許多個(gè)class中查找年齡大于18的對象呢?
6. 實(shí)戰(zhàn)提升
在許多個(gè)class中查找年齡大于18的名字并返回集合。
原始代碼:
List<String> nameList = new ArrayList<>();
for (Class c:classList){
for (Student student:c.getStudents()){
if (student.getAge()>18){
String name = student.getName();
nameList.add(name);
}
}
}
System.out.println(nameList);
鏈?zhǔn)搅鞔a:
如果讓你去寫,你可能會(huì)classList.stream().forEach(aClass -> aClass.getStudents().stream())....去實(shí)現(xiàn)?
我剛開始就是這樣無腦干的,后來我緩過神來,想起foreach是一個(gè)及早求值操作,而且返回值是void,這樣的開頭就注定了沒有結(jié)果,然后仔細(xì)想想,flatMap不是用來處理不是一個(gè)集合的流嗎,好了,就有了下面的代碼。
List<String> collect = classList.stream()
.flatMap(aclass -> aclass.getStudents().stream())
.filter(student -> student.getAge() > 18)
.map(Student::getName)
.collect(toList());
原始代碼和流鏈?zhǔn)秸{(diào)用相比,有以下缺點(diǎn):
- 代碼可讀性差,隱匿了真正的業(yè)務(wù)邏輯
- 需要設(shè)置無關(guān)變量來保存中間結(jié)果
- 效率低,每一步都及早求值生成新集合
- 難于并行化處理
7. 高階函數(shù)及注意事項(xiàng)
高階函數(shù)是指接受另外一個(gè)函數(shù)作為參數(shù),或返回一個(gè)函數(shù)的函數(shù)。如果函數(shù)的函數(shù)里包含接口或返回一個(gè)接口,那么該函數(shù)就是高階函數(shù)。
Stream接口中幾乎所有的函數(shù)都是高階函數(shù)。比如:Comparing 接受一個(gè)函數(shù)作為參數(shù),然后返回Comparator接口。
Student student = list.stream().max(Comparator.comparing(Student::getAge)).get();
public interface Comparator<T> {}
foreach方法也是高階函數(shù):
void forEach(Consumer<? super T> action);
public interface Consumer<T> {}
除了以上還有一些類似功能的高階函數(shù)外,不建議將lambda表達(dá)式傳給Stream上的高階函數(shù),因?yàn)榇蟛糠值母唠A函數(shù)這樣用會(huì)有一些副作用:給變量賦值。局部變量給成員變量賦值,導(dǎo)致很難察覺。
這里拿ActionEvent舉例子:
