歡迎您光臨本站 註冊首頁

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

←手機掃碼閱讀     ml5rwbikls @ 2020-06-01 , reply:0

什麼是Stream

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

如何獲得Stream實例

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

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

StreamstringStream = Stream.of("1", "2", "3"); //無限長的偶數流 StreamevenNumStream = Stream.iterate(0, n -> n + 2); ListstrList = new ArrayList<>(); strList.add("1"); strList.add("2"); strList.add("3"); StreamstrStream = strList.stream(); StreamstrParallelStream = strList.parallelStream();


filter

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

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


map

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

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


mapToInt mapToLong mapToDouble

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

StreamstringStream = 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),如果要得到所有訂單的所有商品彙總,就可以使用該方法,如下:

StreamallItemStream = orderList.stream().flatMap(order -> order.itemList.stream());


flatMapToInt flatMapToLong flatMapToDouble

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

distinct

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

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


sorted

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

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


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

StreamunorderedStream = Stream.of("1234", "123", "12", "12345", "123456", "1"); //按字符串長度從小到大排序完成的流,orderedStream -> ("1", "12", "123", "1234", "12345", "123456") StreamorderedStream = 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: three Mapped value: THREE Filtered value: four Mapped value: FOUR


limit(long maxSize)

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

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


skip(long n)

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

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


forEach

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

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


forEachOrdered

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

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


toArray

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

StreamstringStream = Stream.of("-2", "-1", "0", "1", "2", "3"); Object[] objArray = stringStream.toArray();


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

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


reduce

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

第一個reduce(BinaryOperatoraccumulator)

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();


StreamnumStream = Stream.of(-2, -1, 0, 1, 2, 3); //查找最小值 Optionalmin = numStream.reduce(BinaryOperator.minBy(Integer::compareTo)); //輸出 -2 System.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.empty System.out.println(min);


第二個reduce(T identity, BinaryOperatoraccumulator)

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

單線程等價如下代碼

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


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


第三個reduce(U identity, BiFunction                         accumulator, BinaryOperator combiner)        

identity和accumulator同上

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

StreamnumStream = 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 + -1 accumulator執行:0 + 1 accumulator執行:0 + 0 accumulator執行:0 + 2 accumulator執行:0 + -2 accumulator執行:0 + 3 combiner執行:2 + 3 combiner執行:-1 + 0 combiner執行:1 + 5 combiner執行:-2 + -1 combiner執行:-3 + 6 最終結果:3


collect

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

第一個collect(Suppliersupplier, BiConsumeraccumulator, BiConsumercombiner)

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

accumulator 用於累進計算用

combiner 用於多線程合併結果

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

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


第二個collect(Collector collector)

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

StreamnumStream = Stream.of(-2, -1, 0, 1, 2, 3); ListnumList = numStream.collect(Collectors.toList()); SetnumSet = numStream.collect(Collectors.toSet());


min

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

StreamnumStream = Stream.of(-2, -1, 0, 1, 2, 3); Optionalmin = numStream.min(Integer::compareTo);


max

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

StreamnumStream = Stream.of(-2, -1, 0, 1, 2, 3); Optionalmax = numStream.max(Integer::compareTo);


count

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

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


anyMatch

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

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


allMatch

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

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


noneMatch

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

StreamnumStream = Stream.of(-2, -1, 0, 1, 2, 3); //判斷是否沒有小於0的元素,nOnegativeNum=false boolean nOnegativeNum= numStream.noneMatch(num -> num<0);

findFirst

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

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


findAny

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

StreamnumStream = Stream.of(-2, -1, 0, 1, 2, 3); OptionalanyNum = numStream.findAny();



[ml5rwbikls ] Java8新特性Stream的完全使用指南已經有234次圍觀

http://coctec.com/docs/java/show-post-236497.html