[java]Stream API——collect、reduce、orElse(x)


一、collect

 

1、R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner)

supplier:一個能創造目標類型實例的方法。

accumulator:一個將當元素添加到目標中的方法。

combiner:一個將中間狀態的多個結果整合到一起的方法(並發的時候會用到)

List result = stream.collect(() -> new ArrayList<>(), (list, item) -> list.add(item), (one, two) -> one.addAll(two));

 

2、R collect(Collector collector)

Collector其實是上面supplier、accumulator、combiner的聚合體。那么上面代碼就變成:

List list = Stream.of(1, 2, 3, 4).filter(p -> p > 2).collect(Collectors.toList());

 

3、Collector

Collector是Stream的可變減少操作接口,Collectors(類收集器)提供了許多常見的可變減少操作的實現。

 

4、創建Collector

a.轉換成其他集合:toList、toSet、toCollection、toMap

List<Integer> collectList = Stream.of(1, 2, 3, 4)
        .collect(Collectors.toList());
System.out.println("collectList: " + collectList); // 打印結果 // collectList: [1, 2, 3, 4]

 b.轉成值:

使用collect可以將Stream轉換成值。maxBy和minBy允許用戶按照某個特定的順序生成一個值。

    • averagingDouble:求平均值,Stream的元素類型為double
    • averagingInt:求平均值,Stream的元素類型為int
    • averagingLong:求平均值,Stream的元素類型為long
    • counting:Stream的元素個數
    • maxBy:在指定條件下的,Stream的最大元素
    • minBy:在指定條件下的,Stream的最小元素
    • reducing: reduce操作
    • summarizingDouble:統計Stream的數據(double)狀態,其中包括count,min,max,sum和平均。
    • summarizingInt:統計Stream的數據(int)狀態,其中包括count,min,max,sum和平均。
    • summarizingLong:統計Stream的數據(long)狀態,其中包括count,min,max,sum和平均。
    • summingDouble:求和,Stream的元素類型為double
    • summingInt:求和,Stream的元素類型為int
    • summingLong:求和,Stream的元素類型為long 

例:

Optional<Integer> collectMaxBy = Stream.of(1, 2, 3, 4)
            .collect(Collectors.maxBy(Comparator.comparingInt(o -> o))); System.out.println("collectMaxBy:" + collectMaxBy.get()); // 打印結果 // collectMaxBy:4

c.分割數據塊:Collectors.partitioningBy

 Map<Boolean, List<Integer>> collectParti = Stream.of(1, 2, 3, 4)
            .collect(Collectors.partitioningBy(it -> it % 2 == 0)); System.out.println("collectParti : " + collectParti); // 打印結果 // collectParti : {false=[1, 3], true=[2, 4]}

d.數據分組:Collectors.groupingBy 

Map<Boolean, List<Integer>> collectGroup= Stream.of(1, 2, 3, 4)
            .collect(Collectors.groupingBy(it -> it > 3)); System.out.println("collectGroup : " + collectGroup); // 打印結果 // collectGroup : {false=[1, 2, 3], true=[4]}

e.字符串:Collectors.joining

String strJoin = Stream.of("1", "2", "3", "4")
        .collect(Collectors.joining(",", "[", "]")); System.out.println("strJoin: " + strJoin); // 打印結果 // strJoin: [1,2,3,4]

二、聚合操作reduce

T reduce(T identity, BinaryOperator accumulator)

代碼:

int value = Stream.of(1, 2, 3, 4).reduce(100, (sum, item) -> sum + item);

或者使用方法引用

int value = Stream.of(1, 2, 3, 4).reduce(100, Integer::sum);

value結果:101,103,106,110

T reduce(T identity, BinaryOperator accumulator)

  • identity:它允許用戶提供一個循環計算的初始值。(例中的100)
  • accumulator:計算的累加器,其方法簽名為apply(T t,U u),在該reduce方法中第一個參數t(例中的sum)為上次函數計算的返回值,第二個參數u(例中的item)為Stream中的元素,這個函數把這兩個值計算apply,得到的和會被賦值給下次執行這個方法的第一個參數。

三、orElse(x):屬於Stream終結操作,與findFirst()組合使用,返回對象,如果沒有,則返回x。

StCompanySetting stCompanySetting = stCompanySettingDao.selectAll().stream().findFirst().orElse(null);//如果沒有,則返回null


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM