深入理解Java8新特性之Stream API的創(chuàng)建方式和中間操作步驟
1.什么是StreamAPI?
Java8中有兩大最為重要的改變。第一個是 Lambda 表達(dá)式;另外一個則是 m Stream API (java.util.stream.*) 。
Stream 是 Java8 中處理集合的關(guān)鍵抽象概念,它可以指定你希望對集合進行的操作,可以執(zhí)行非常復(fù)雜的查找、過濾和映射數(shù)據(jù)等操作。
使用Stream API 對集合數(shù)據(jù)進行操作,就類似于使用 SQL 執(zhí)行的數(shù)據(jù)庫查詢。也可以使用 Stream API 來并行執(zhí)行操作。簡而言之,Stream API 提供了一種高效且易于使用的處理數(shù)據(jù)的方式。
流 (Stream) 到底是什么 呢 ?
是數(shù)據(jù)渠道,用于操作數(shù)據(jù)源(集合、數(shù)組等)所生成的元素序列?!?集合講的是 數(shù)據(jù) , 流講的是 計算 !”
注意 :
- Stream 自己不會存儲元素。
- Stream 不會改變源對象。相反,他們會返回一個持有結(jié)果的新Stream。
- Stream 操作是延遲執(zhí)行的。這意味著他們會等到需要結(jié)果的時候才執(zhí)行。
2.Stream API操作的三個步驟
- 創(chuàng)建Stream:一個數(shù)據(jù)源(如:集合、數(shù)組),獲取一個流。
- 中間操作:一個中間操作鏈,對數(shù)據(jù)源的數(shù)據(jù)進行處理。
- 終止操作( ( 終端操作) ):一個終止操作,執(zhí)行中間操作鏈,并產(chǎn)生結(jié)果。

2.1 創(chuàng)建Stream
在Java8 中, Collection 接口被擴展,提供了兩個獲取流的方法 :
- default Stream<E> stream() : 返回一個順序流
- default Stream<E> parallelStream() : 返回一個并行流
Java8 中的 Arrays 的靜態(tài)方法 stream() 可以獲取數(shù)組流:
- static <T> Stream<T> stream(T[] array): 返回一個流重載形式 , 能夠處理對應(yīng)基本類型的數(shù)組 :
- public static IntStream stream(int[] array)
- public static LongStream stream(long[] array)
- public static DoubleStream stream(double[] array)
可以使用靜態(tài)方法 Stream.of(),通過顯示值創(chuàng)建一個流。它可以接收任意數(shù)量的參數(shù)。
- public static<T> Stream<T> of(T... values) : 返回一個流
可以使用靜態(tài)方法 Stream.iterate() 和 Stream.generate(),創(chuàng)建無限流。
- 迭代 public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
- 生成 public static<T> Stream<T> generate(Supplier<T> s)
//創(chuàng)建Stream
@Test
public void test1() {
//1.Collection 提供了兩個方法 stream() 與 parallelStream()
List<String> list = new ArrayList<>();
Stream<String> stream1 = list.stream();
//2. 通過 Arrays 中的 stream() 獲取一個數(shù)組流
String[] strings = new String[10];
Stream<String> stream2 = Arrays.stream(strings);
//3. 通過 Stream 類中靜態(tài)方法 of()
Stream<String> stream3 = Stream.of("abc","666","???");
//4. 創(chuàng)建無限流
//迭代
Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 2);
stream4.limit(5).forEach(System.out::println);
//生成
Stream.generate(() -> Math.random())
.limit(5)
.forEach(System.out::println);
}

2.2 中間操作
多個 中間操作可以連接起來形成一個 流 水 線,除非流水線上觸發(fā)終止操作,否則 中 間操作 不 會執(zhí)行 任 何 的 處 理!
而在 終止操作時一次性全部處理 , 稱 為 “ 惰 性 求 值 ”。 常用的中間操作大體上可以分為三類:①篩選與切片;②映射;③排序
2.2.1 中間操作之篩選與切片

下面通過一些代碼案例來應(yīng)用一下。 首先,我們準(zhǔn)備一個Employee類,以及一個List集合。
package com.szh.java8;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Integer id;
private String name;
private Integer age;
private Double salary;
public Employee(Integer id) {
this.id = id;
}
public Employee(Integer id,String name) {
this.id = id;
this.name = name;
}
}
List<Employee> employees = Arrays.asList(
new Employee(1001,"張三",26,6666.66),
new Employee(1002,"李四",50,1111.11),
new Employee(1003,"王五",18,9999.99),
new Employee(1004,"趙六",35,8888.88),
new Employee(1005,"田七",44,3333.33),
new Employee(1005,"田七",44,3333.33),
new Employee(1005,"田七",44,3333.33)
);
篩選年齡小于35歲的員工信息。 這里每次都是先 println 打印結(jié)果,然后進行比較,滿足則輸出。
//內(nèi)部迭代,在 Stream API 內(nèi)部完成
@Test
public void test2() {
//所有的中間操作不會做任何的處理
Stream<Employee> stream = employees.stream()
.filter((e) -> {
System.out.println("測試中間操作");
return e.getAge() <= 35;
});
//只有當(dāng)做終止操作時,所有的中間操作會一次性的全部執(zhí)行,稱為“惰性求值”
stream.forEach(System.out::println);
}

篩選薪資大于5000的員工信息,但是只保留前兩條。
@Test
public void test3() {
employees.stream()
.filter((e) -> {
System.out.println("短路....");
return e.getSalary() > 5000;
})
.limit(2)
.forEach(System.out::println);
}

篩選薪資大于5000的員工信息,但是扔掉前兩條。
@Test
public void test4() {
employees.stream()
.filter((e) -> e.getSalary() > 5000)
.skip(2)
.forEach(System.out::println);
}

對集合中的所有員工信息做去重處理。
@Test
public void test5() {
employees.stream()
.distinct()
.forEach(System.out::println);
}

2.2.2 中間操作之映射

這里仍然是借助上面案例中的Employee類、List集合完成。
首先對自定義的List集合中的所有字符串進行大寫轉(zhuǎn)換,同時對員工集合進行 name 字段的映射處理。
@Test
public void test1() {
List<String> list = Arrays.asList("aaa","bbb","ccc","ddd","eee");
list.stream()
.map((str) -> str.toUpperCase())
.forEach(System.out::println);
System.out.println("--------------------------------------");
employees.stream()
.map(Employee::getName)
.forEach(System.out::println);
}

對List集合中的每個字符串中的每個字符進行遍歷輸出。(①使用map;②使用flatMap)
這兩種方式主要區(qū)別就在獲取到Stream流之后,流中的泛型不一樣了。
我們使用map了話,它會將strList集合中元素以 "aaa"、"bbb"、"ccc" 這樣的形式返回,此時Stream流的泛型就仍然是一個Stream流(在這個Stream流中才是我們想要的數(shù)據(jù)),也就是我們第一次對stream流進行遍歷之后,拿到的是一個又一個的Character字符數(shù)組,然后還需要再對這個字符數(shù)組進行二次遍歷(也就是代碼上半部分中那兩個 forEach)。
如果我們使用flatMap了話,它會將strList集合中元素以 "a'、"b"、"c"、"d" 這樣的形式返回,所以此時Stream流的泛型就只是一個Character字符數(shù)組,那么一次遍歷就可以取出數(shù)據(jù)了(也就是代碼下半部分的那個forEach)。
@Test
public void test2() {
List<String> strList = Arrays.asList("aaa","bbb","ccc","ddd","eee");
Stream<Stream<Character>> stream1 = strList.stream()
.map(MyTest6::filterCharacter);
stream1.forEach((sm) -> {
sm.forEach(System.out::println);
});
System.out.println("----------------------------------------");
Stream<Character> stream2 = strList.stream()
.flatMap(MyTest6::filterCharacter);
stream2.forEach(System.out::println);
}
private static Stream<Character> filterCharacter(String str) {
List<Character> list = new ArrayList<>();
for (Character character : str.toCharArray()) {
list.add(character);
}
return list.stream();
}

2.2.3 中間操作之排序

對List集合中的字符串進行自然排序。對員工集合中的員工信息進行定制化排序(先按照年齡排序,年齡一樣再按照姓名排序)。
@Test
public void test3() {
List<String> strList = Arrays.asList("ccc","aaa","bbb","eee","ddd");
strList.stream()
.sorted()
.forEach(System.out::println);
System.out.println("----------------------------------------");
employees.stream()
.sorted((e1,e2) -> {
if (e1.getAge().equals(e2.getAge())) {
return e1.getName().compareTo(e2.getName());
} else {
return e1.getAge().compareTo(e2.getAge());
}
})
.forEach(System.out::println);
}

以上就是深入理解Java8新特性之Stream API的創(chuàng)建方式和中間操作步驟的詳細(xì)內(nèi)容,更多關(guān)于Java Stream API的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Spring中使用xml配置bean的細(xì)節(jié)
本篇文章主要介紹了Spring中使用xml配置bean的細(xì)節(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
MyBatis游標(biāo)Cursor在Oracle數(shù)據(jù)庫上的測試方式
這篇文章主要介紹了MyBatis游標(biāo)Cursor在Oracle數(shù)據(jù)庫上的測試方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
帶有@Transactional和@Async的循環(huán)依賴問題的解決
這篇文章主要介紹了帶有@Transactional和@Async的循環(huán)依賴問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

