map原理
map原理
map內(nèi)部函數(shù)接口依賴
filter原理
filter原理
filter內(nèi)部函數(shù)接口依賴
flatMap原理
flatMap原理
reduce操作累加
reduce操作累加
package org.java8.stream;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;
public class StreamMethodTest {
public static void main(String[] args) {
//初始化list
List<String> list = asList("one", "two", "three", "four");
Stream.of("one", "two", "three", "four").collect(toList());
/**map:**/
/**map功能(將一種類型數(shù)據(jù)的值轉(zhuǎn)化成另一種類型)
//比如將小寫轉(zhuǎn)成大寫
//注意map的函數(shù)接口是Function壤圃,一進(jìn)一出變個(gè)身**/
List<String> mapList = Stream.of("one", "two", "three", "four")
.map(value->value.toUpperCase())
.collect(toList());
//forEach內(nèi)部函數(shù)接口是Consumer念链,只進(jìn)不出消化了
mapList.forEach(x->System.out.println(x));
/**filter:**/
/**遍歷對(duì)數(shù)據(jù)進(jìn)行檢查用filter我磁,一進(jìn)出來(lái)成boolean。其實(shí)就是for里面的if語(yǔ)句
filter內(nèi)部的函數(shù)接口是Predicate**/
List<String> filterList = Stream.of("one", "two", "three", "four")
.filter(x->x.equalsIgnoreCase("one"))
.collect(toList());
filterList.forEach(x->System.out.println(x));
/**flatMap:**/
//有時(shí):對(duì)map中的值用新的stream來(lái)替換他
List<Integer> together = Stream.of(asList(1, 2), asList(3, 4))
.flatMap(numbers -> numbers.stream())
.collect(toList());
together.forEach(x->System.out.print(x));
System.out.println();
/**reduce:**/
/** reduce操作可以實(shí)現(xiàn)從一組值中生成一個(gè)值
如何通過(guò)reduce操作對(duì)Stream中的數(shù)字求和浮定。以0作起點(diǎn)——一個(gè)空流
Stream的求和結(jié)果,每一步都將Stream中的元素累加至accumulator,遍歷至Stream中的
最后一個(gè)元素時(shí),accumulator的值就是所有元素的和燎含。**/
/**BinaryOperator reduce包含初始值和BinaryOperator函數(shù)接口**/
int count = Stream.of(1, 2, 3)
.reduce(0, (acc, element) -> acc + element);
System.out.println(count);
/**展開(kāi)過(guò)程**/
BinaryOperator<Integer> accumulator = (acc, element) -> acc + element;
int anotherCount = accumulator.apply(
accumulator.apply(
accumulator.apply(0, 1),
2),
3);
System.out.println(anotherCount);
/**等同功能**/
int acc = 0;
for (Integer element : asList(1, 2, 3)) {
acc = acc + element;
}
System.out.println(acc);
/**max和min: 其實(shí)包含了reduce功能:reduce(BinaryOperator.minBy(comparator));**/
List<Track> tracks = asList(new Track("Bakai", 524),
new Track("Violets for Your Furs", 378),
new Track("Time Was", 451));
Track shortestTrack = tracks.stream()
.min(Comparator.comparing(track -> track.getLength()))
.get();
System.out.println(shortestTrack);
}
static class Track {
String name;
int length;
public Track(String name, int length) {
this.name = name;
this.length = length;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Track [name=").append(name).append(", length=")
.append(length).append("]");
return builder.toString();
}
}
}