您的位置:首頁技術文章
文章詳情頁

Java8新特性Stream的完全使用指南

瀏覽:3日期:2022-08-31 18:29:33

什么是Stream

Stream是Java 1.8版本開始提供的一個接口,主要提供對數據集合使用流的方式進行操作,流中的元素不可變且只會被消費一次,所有方法都設計成支持鏈式調用。使用Stream API可以極大生產力,寫出高效率、干凈、簡潔的代碼。

如何獲得Stream實例

Stream提供了靜態構建方法,可以基于不同的參數創建返回Stream實例

使用Collection的子類實例調用stream()或者parallelStream()方法也可以得到Stream實例,兩個方法的區別在于后續執行Stream其他方法的時候是單線程還是多線程

Stream<String> stringStream = Stream.of('1', '2', '3');//無限長的偶數流Stream<Integer> evenNumStream = Stream.iterate(0, n -> n + 2);List<String> strList = new ArrayList<>();strList.add('1');strList.add('2');strList.add('3');Stream<String> strStream = strList.stream();Stream<String> strParallelStream = strList.parallelStream();

filter

filter方法用于根據指定的條件做過濾,返回符合條件的流

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//獲得只包含正數的流,positiveNumStream -> (1,2,3)Stream<Integer> positiveNumStream = numStream.filter(num -> num > 0);

map

map方法用于將流中的每個元素執行指定的轉換邏輯,返回其他類型元素的流

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//轉換成字符串流Stream<String> strStream = numStream.map(String::valueOf);

mapToInt mapToLong mapToDouble

這三個方法是對map方法的封裝,返回的是官方為各個類型單獨定義的Stream,該Stream還提供了適合各自類型的其他操作方法

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');IntStream intStream = stringStream.mapToInt(Integer::parseInt);LongStream longStream = stringStream.mapToLong(Long::parseLong);DoubleStream doubleStream = stringStream.mapToDouble(Double::parseDouble);

flatMap

flatMap方法用于將流中的每個元素轉換成其他類型元素的流,比如,當前有一個訂單(Order)列表,每個訂單又包含多個商品(itemList),如果要得到所有訂單的所有商品匯總,就可以使用該方法,如下:

Stream<Item> allItemStream = orderList.stream().flatMap(order -> order.itemList.stream());

flatMapToInt flatMapToLong flatMapToDouble

這三個方法是對flatMap方法的封裝,返回的是官方為各個類型單獨定義的Stream,使用方法同上

distinct

distinct方法用于對流中的元素去重,判斷元素是否重復使用的是equals方法

Stream<Integer> numStream = Stream.of(-2, -1, 0, 0, 1, 2, 2, 3);//不重復的數字流,uniqueNumStream -> (-2, -1, 0, 1, 2, 3)Stream<Integer> uniqueNumStream = numStream.distinct();

sorted

sorted有一個無參和一個有參的方法,用于對流中的元素進行排序。無參方法要求流中的元素必須實現Comparable接口,不然會報java.lang.ClassCastException異常

Stream<Integer> unorderedStream = Stream.of(5, 6, 32, 7, 27, 4);//按從小到大排序完成的流,orderedStream -> (4, 5, 6, 7, 27, 32)Stream<Integer> orderedStream = unorderedStream.sorted();

有參方法sorted(Comparator<? super T> comparator)不需要元素實現Comparable接口,通過指定的元素比較器對流內的元素進行排序

Stream<String> unorderedStream = Stream.of('1234', '123', '12', '12345', '123456', '1');//按字符串長度從小到大排序完成的流,orderedStream -> ('1', '12', '123', '1234', '12345', '123456')Stream<String> orderedStream = unorderedStream.sorted(Comparator.comparingInt(String::length));

peek

peek方法可以不調整元素順序和數量的情況下消費每一個元素,然后產生新的流,按文檔上的說明,主要是用于對流執行的中間過程做debug的時候使用,因為Stream使用的時候一般都是鏈式調用的,所以可能會執行多次流操作,如果想看每個元素在多次流操作中間的流轉情況,就可以使用這個方法實現

Stream.of('one', 'two', 'three', 'four') .filter(e -> e.length() > 3) .peek(e -> System.out.println('Filtered value: ' + e)) .map(String::toUpperCase) .peek(e -> System.out.println('Mapped value: ' + e)) .collect(Collectors.toList()); 輸出:Filtered value: threeMapped value: THREEFiltered value: fourMapped value: FOUR

limit(long maxSize)

limit方法會對流進行順序截取,從第1個元素開始,保留最多maxSize個元素

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');//截取前3個元素,subStringStream -> ('-2', '-1', '0')Stream<String> subStringStream = stringStream.limit(3);

skip(long n)

skip方法用于跳過前n個元素,如果流中的元素數量不足n,則返回一個空的流

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');//跳過前3個元素,subStringStream -> ('1', '2', '3')Stream<String> subStringStream = stringStream.skip(3);

forEach

forEach方法的作用跟普通的for循環類似,不過這個可以支持多線程遍歷,但是不保證遍歷的順序

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');//單線程遍歷輸出元素stringStream.forEach(System.out::println);//多線程遍歷輸出元素stringStream.parallel().forEach(System.out::println);

forEachOrdered

forEachOrdered方法可以保證順序遍歷,比如這個流是從外部傳進來的,然后在這之前調用過parallel方法開啟了多線程執行,就可以使用這個方法保證單線程順序遍歷

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');//順序遍歷輸出元素stringStream.forEachOrdered(System.out::println);//多線程遍歷輸出元素,下面這行跟上面的執行結果是一樣的//stringStream.parallel().forEachOrdered(System.out::println);

toArray

toArray有一個無參和一個有參的方法,無參方法用于把流中的元素轉換成Object數組

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');Object[] objArray = stringStream.toArray();

有參方法toArray(IntFunction<A[]> generator)支持把流中的元素轉換成指定類型的元素數組

Stream<String> stringStream = Stream.of('-2', '-1', '0', '1', '2', '3');String[] strArray = stringStream.toArray(String[]::new);

reduce

reduce有三個重載方法,作用是對流內元素做累進操作

第一個reduce(BinaryOperator<T> accumulator)

accumulator 為累進操作的具體計算

單線程等下如下代碼

boolean foundAny = false;T result = null;for (T element : this stream) { if (!foundAny) { foundAny = true; result = element; } else result = accumulator.apply(result, element);}return foundAny ? Optional.of(result) : Optional.empty();

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//查找最小值Optional<Integer> min = numStream.reduce(BinaryOperator.minBy(Integer::compareTo));//輸出 -2System.out.println(min.get());//過濾出大于5的元素流numStream = Stream.of(-2, -1, 0, 1, 2, 3).filter(num -> num > 5);//查找最小值min = numStream.reduce(BinaryOperator.minBy(Integer::compareTo));//輸出 Optional.emptySystem.out.println(min);

第二個reduce(T identity, BinaryOperator<T> accumulator)

identity 為累進操作的初始值accumulator 同上

單線程等價如下代碼

T result = identity;for (T element : this stream) result = accumulator.apply(result, element)return result;

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//累加計算所有元素的和,sum=3int sum = numStream.reduce(0, Integer::sum);

第三個reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)

identity和accumulator同上

combiner用于多線程執行的情況下合并最終結果

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);int sum = numStream.parallel().reduce(0, (a, b) -> { System.out.println('accumulator執行:' + a + ' + ' + b); return a + b;}, (a, b) -> { System.out.println('combiner執行:' + a + ' + ' + b); return a + b;});System.out.println('最終結果:'+sum);輸出:accumulator執行:0 + -1accumulator執行:0 + 1accumulator執行:0 + 0accumulator執行:0 + 2accumulator執行:0 + -2accumulator執行:0 + 3combiner執行:2 + 3combiner執行:-1 + 0combiner執行:1 + 5combiner執行:-2 + -1combiner執行:-3 + 6最終結果:3

collect

collect有兩個重載方法,主要作用是把流中的元素作為集合轉換成其他Collection的子類,其內部實現類似于前面的累進操作

第一個collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner)

supplier 需要返回開始執行時的默認結果

accumulator 用于累進計算用

combiner 用于多線程合并結果

單線程執行等價于如下代碼

R result = supplier.get();for (T element : this stream) accumulator.accept(result, element);return result;

第二個collect(Collector<? super T, A, R> collector)

collector其實是對上面的方法參數的一個封裝,內部執行邏輯是一樣的,只不過JDK提供了一些默認的Collector實現

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);List<Integer> numList = numStream.collect(Collectors.toList());Set<Integer> numSet = numStream.collect(Collectors.toSet());

min

min方法用于計算流內元素的最小值

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);Optional<Integer> min = numStream.min(Integer::compareTo);

max

min方法用于計算流內元素的最大值

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);Optional<Integer> max = numStream.max(Integer::compareTo);

count

count方法用于統計流內元素的總個數

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//count=6long count = numStream.count();

anyMatch

anyMatch方法用于匹配校驗流內元素是否有符合指定條件的元素

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//判斷是否包含正數,hasPositiveNum=trueboolean hasPositiveNum = numStream.anyMatch(num -> num > 0);

allMatch

allMatch方法用于匹配校驗流內元素是否所有元素都符合指定條件

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//判斷是否全部是正數,allNumPositive=falseboolean allNumPositive = numStream.allMatch(num -> num > 0);

noneMatch

noneMatch方法用于匹配校驗流內元素是否都不符合指定條件

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//判斷是否沒有小于0的元素,noNegativeNum=falseboolean noNegativeNum = numStream.noneMatch(num -> num < 0);

findFirst

findFirst方法用于獲取第一個元素,如果流是空的,則返回Optional.empty

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);//獲取第一個元素,firstNum=-2Optional<Integer> firstNum = numStream.findFirst();

findAny

findAny方法用于獲取流中的任意一個元素,如果流是空的,則返回Optional.empty,因為可能會使用多線程,所以不保證每次返回的是同一個元素

Stream<Integer> numStream = Stream.of(-2, -1, 0, 1, 2, 3);Optional<Integer> anyNum = numStream.findAny();

總結

到此這篇關于Java8新特性Stream的完全使用指南就介紹到這了,更多相關Java8 Stream使用指南內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
国产综合久久一区二区三区