Java流(Stream)操作自Java 8引入蚀浆,通過Stream操作可以簡化代碼編寫,提高代碼執(zhí)行效率搜吧。流整體操作分為創(chuàng)建(Supplier)市俊、轉(zhuǎn)換、約簡和收集幾個操作滤奈。
創(chuàng)建流
創(chuàng)建流最常見的方式是調(diào)用Collection.stream()方法摆昧,將集合數(shù)據(jù)轉(zhuǎn)換為流,也可以通過generate方法創(chuàng)建流蜒程。如下所示:
public static void main(String[] args) {
//從Collection中產(chǎn)生流
ArrayList<String> strings = new ArrayList<>();
for (int i = 0; i < 20; ++i) {
strings.add("string" + i);
}
Stream<String> stringStream = strings.stream();
show("stringStream", stringStream);
//使用generate產(chǎn)生流
Stream<Integer> integerStream = Stream.generate(() -> (int) (Math.random() * 100));
show("integerStream", integerStream);
//使用iterate產(chǎn)生序列
Stream<Integer> integerStream1 = Stream.iterate(0, n -> n + 1);
show("integerStream1", integerStream1);
//使用of方法創(chuàng)建流
Stream<String> stringStream1 = Stream.of("Bob", "Mark", "Alice", "Edd");
show("stringStream1", stringStream1);
//正則表達(dá)式產(chǎn)生流
String sentences = "I had a dog whose name was Bingo. B, I, N, G, O, Bingo was his name, Oh!";
Stream<String> words = Pattern.compile("\\PL+").splitAsStream(sentences);
show("PatterStream", words);
}
public static<T> void show(String title, Stream<T> stream) {
final int size = 10;
List<T> array = stream.limit(size).collect(Collectors.toList());
System.out.println(title + ": ");
for (int i = 0; i < array.size(); ++i) {
if (i > 0) {
System.out.print(", ");
}
if (i < array.size()) {
System.out.print(array.get(i));
} else {
System.out.print("...");
}
}
System.out.println();
}
流的轉(zhuǎn)換
流的轉(zhuǎn)換即將一個給定的流通過流的方法轉(zhuǎn)換為另一個流绅你,常見的方法有filter,map昭躺,distinct, sorted忌锯,reverse等方法。
- filter方法保留滿足filter方法中的條件表達(dá)式的元素领炫;
- map方法可以對流中的元素進(jìn)行一個轉(zhuǎn)換操作偶垮;
- distinct方法將流中重復(fù)的元素去除
- sorted按給定的比較方法進(jìn)行排序
String sentences = "I had a dog whose name was Bingo. B, I, N, G, O, Bingo was his name, Oh!";
//保留長度大于2的單詞
Stream<String> words1 = Pattern.compile("\\PL+").splitAsStream(sentences).filter(s -> s.length() > 2);
show("PatterStream1", words1);
//將單詞轉(zhuǎn)換為大寫
Stream<String> words2 = Pattern.compile("\\PL+").splitAsStream(sentences).map(String::toUpperCase);
show("PatterStream2", words2);
//去除重復(fù)的單詞
Stream<String> words3 = Pattern.compile("\\PL+").splitAsStream(sentences).distinct();
show("PatterStream3", words3);
//按長度對單詞進(jìn)行排序
Stream<String> words4 = Pattern.compile("\\PL+").splitAsStream(sentences).sorted(Comparator.comparing(String::length));
show("PatterStream4", words4);
//按長度對單詞進(jìn)行排序后反序
Stream<String> words5 = Pattern.compile("\\PL+").splitAsStream(sentences).sorted(Comparator.comparing(String::length).reversed());
show("PatterStream4", words5);
流的約減(Reduce)
流經(jīng)過處理后,提供了一系列方法從流中抽取數(shù)據(jù)。抽取數(shù)據(jù)是一種終結(jié)操作针史,常用的方法如下:
- count方法:獲取流中滿足條件的元素個數(shù)
- min晶伦、max方法:按照給定的比較方法獲取最小最大值,注意該方法返回的是一個Optional<T>的包裝類啄枕,這是因?yàn)榱骺赡転榭铡?/li>
- find查找方法:包括findFirst婚陪,findAny等。
- match方法:根據(jù)條件查找是否有匹配條件的元素频祝,有anyMatch泌参,allMatch和noneMatch,返回的是boolean類型常空。
- 消費(fèi)流:通過方法消費(fèi)流中的元素沽一,例如使用forEach迭代,傳遞方法處理流中的每個元素漓糙。
- 轉(zhuǎn)換為Collection或數(shù)組:通過collect方法傳遞Collectors類提供的toCollection方法可以轉(zhuǎn)換為Collection或者調(diào)用toArray方法轉(zhuǎn)換為數(shù)組铣缠。
- 拼接:使用Collectors.join方法可以拼接流.
- 統(tǒng)計,使用IntSummaryStatistics昆禽、DoubleSummaryStatistics和LongSummaryStatistics方法構(gòu)造統(tǒng)計對象蝗蛙,獲取 總數(shù),平均值醉鳖,最大值捡硅,最小值。需要注意調(diào)用Collectors.summarizing{Int,Double, Long}方法需要傳遞一個lambda表達(dá)式盗棵,確定統(tǒng)計的內(nèi)容壮韭。
//獲取單詞的數(shù)量
long wordsCount = Pattern.compile("\\PL+").splitAsStream(sentences).filter(s -> s.length() > 2).count();
System.out.println("Number of words length greater than 2: " + wordsCount);
//獲取最長的單詞
Optional<String> longestWord = Pattern.compile("\\PL+").splitAsStream(sentences).max(Comparator.comparing(String::length));
System.out.println("longestWord: " + longestWord.orElse("null"));
//獲取最短的單詞
Optional<String> shortestWord = Pattern.compile("\\PL+").splitAsStream(sentences).min(Comparator.comparing(String::length));
System.out.println("shortestWord: " + shortestWord.orElse("null"));
//獲取第一個以h開頭的單詞
Optional<String> firstWordsStartWithH = Pattern.compile("\\PL+").splitAsStream(sentences)
.filter(s -> s.toLowerCase().startsWith("h")).findFirst();
System.out.println("firstWordsStartWithH: " + firstWordsStartWithH.orElse("Not Found"));
//獲取任何一個以h開頭的單詞
Optional<String> anyWordsStartWithH = Pattern.compile("\\PL+").splitAsStream(sentences)
.filter(s -> s.toLowerCase().startsWith("h")).findAny();
System.out.println("anyWordsStartWithH: " + anyWordsStartWithH.orElse("Not Found"));
//查找其中是否有was這個單詞
boolean hasWas = Pattern.compile("\\PL+").splitAsStream(sentences).anyMatch("was"::equals);
System.out.println("hasWas: " + hasWas);
//查找其中全部是was這個單詞
boolean allWas = Pattern.compile("\\PL+").splitAsStream(sentences).allMatch("was"::equals);
System.out.println("allWas: " + allWas);
//使用forEach方法打印全部單詞的大寫
Pattern.compile("\\PL+").splitAsStream(sentences).map(String::toUpperCase).forEach(System.out::print);
//將流轉(zhuǎn)為Collection
List<String> wordsList = Pattern.compile("\\PL+").splitAsStream(sentences).collect(Collectors.toList());
System.out.println("wordsList: " + wordsList);
//將流轉(zhuǎn)為數(shù)組
String[] wordsArray = Pattern.compile("\\PL+").splitAsStream(sentences).toArray(String[]::new);
System.out.println("wordsArray: " + Arrays.toString(wordsArray));
//將流拼接為字符串
String wordsToSentences = Pattern.compile("\\PL+").splitAsStream(sentences).collect(Collectors.joining(", "));
System.out.println("wordsToSentences: " + wordsToSentences);
//數(shù)據(jù)統(tǒng)計:統(tǒng)計單詞的長度
IntSummaryStatistics lengthSummary = Pattern.compile("\\PL+").splitAsStream(sentences).
collect(Collectors.summarizingInt(String::length));
System.out.println("Longest word length: " + lengthSummary.getMax());
System.out.println("Shortest word length: " + lengthSummary.getMin());
System.out.println("Average word length: " + lengthSummary.getAverage());
- 收集到Map:使用Collectors.toMap轉(zhuǎn)到Map數(shù)據(jù)中。
- 分組:使用groupingBy進(jìn)行分組
public static class User {
private String name;
private int id;
//1=male, 0 = female, 2= unknown
private int sex = 2;
public User(int id, String name, int sex) {
this.id = id;
this.name = name;
if (sex == 0 || sex == 1) {
this.sex = sex;
}
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getSex() {
return sex;
}
@Override
public String toString() {
return "User{id = " + id + ", name = " + name + ", sex = " + sex + "}";
}
}
//省略部分代碼...
public static Stream<User> userStream() {
return Stream.of(
new User(1, "Box", 1),
new User(2, "Succi", 0),
new User(3, "Lily", 0),
new User(4, "Sara", 0),
new User(5, "Mark", 1)
);
}
//main方法
public static void main(String[] args) {
//...
//映射為id -> User格式
Map<Integer, User> userMap = userStream().collect(Collectors.toMap(User::getId, Function.identity()));
for (Integer key:userMap.keySet()) {
System.out.println("id " + key + ": " + userMap.get(key));
}
//映射為id -> User.name格式
Map<Integer, String> idToNameMap = userStream().collect(Collectors.toMap(User::getId, User::getName));
System.out.println(idToNameMap);
//按性別分組
Map<Integer, Set<User>> sexToUserMap = userStream().collect(Collectors.groupingBy(User::getSex, Collectors.toSet()));
for (Integer key:sexToUserMap.keySet()) {
Set<User> usersOfSex = sexToUserMap.get(key);
System.out.println("Sex " + key + ": " + usersOfSex);
}
}