日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java connot reduce_Java8: Reduce方法

發布時間:2023/12/19 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java connot reduce_Java8: Reduce方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:reduce

rudece方法:從一個流中生成一個值

三個重載方法:

Optional reduce(BinaryOperator accumulator);

T reduce(T identity, BinaryOperator accumulator);

U reduce(U identity,

BiFunction accumulator,

BinaryOperator combiner);

二:重載方法原理

一個參數

接口繼承詳情:

Optional reduce(BinaryOperator accumulator);

@FunctionalInterface

public interface BinaryOperator extends BiFunction {

//兩個靜態方法,先進行忽略

}

@FunctionalInterface

public interface BiFunction {

R apply(T t, U u);

//一個默認方法,先進行忽略

}

這里可以看出,reduce方法參數為一個函數,返回值為Optional對象,BinaryOperator的作用為規定BiFunction的三個參數泛型類型要一致,也就是說只要我們對apply方法進行實現并傳進去就ok了。

文檔中寫到:

Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value。

大致意思:使用累積函數對此流的元素執行操作,并返回一個描述結果的Optional對象。

reduce方法的效果:

This is equivalent to:

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

如上文描述:用T類型對象對流進行遍歷,第一個數值進行賦值,其余apply操作,并將操作的結果進行返回,等待下次繼續當apply方法參數輸入。

那也就是說,我們可以這樣:

求和效果展示:

List num = Arrays.asList(1, 2, 4, 5, 6, 7);

Integer result = num.stream().reduce((x, y) -> {

System.out.println("x:"+x);

return x + y;

}).get();

System.out.println("resutl:"+result);

//你也可以這樣寫,效果一樣,一個為Lambda表達式,一個匿名內部類

Integer integer = num.stream().reduce(new BinaryOperator() {

@Override

public Integer apply(Integer a, Integer b) {

return a + b;

}

}).get();

兩個參數

接口繼承詳情:

該方法的參數多了一個identity,初始值

T reduce(T identity, BinaryOperator accumulator);

文檔解釋:

Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.

大致意思:使用提供的初始值和累計函數對流進行操作,并返回一個初始值類型的計算結果。

T result = identity;

for (T element : this stream){

result = accumulator.apply(result, element)

}

return result;

reduce方法效果:使用identity作為初始值,每遍歷到一個數據,用apply方法操作并將結果返回。

計和效果展示:

List num = Arrays.asList(1, 2, 3, 4, 5, 6);

Integer result = num.stream().reduce(0,(x, y) -> {

System.out.println("x:" + x);

return x + y;

});

System.out.println("resutl:" + result);

不同點:①,reduce中多了一個參數,初始值identity。②方法的返回結果為初始值identity類型的對象。

三個參數

接口繼承詳情:

U reduce(U identity,

BiFunction accumulator,

BinaryOperator combiner);

@FunctionalInterface

public interface BiFunction {

R apply(T t, U u);

//一個默認方法,忽略

}

這里先看一個reduce的參數:①,U類型的初始值。②,(T,U)→U,T+U返回U類型。③組合器,(T,T)→T,T+T返回T類型。

文檔描述:

這個是對于combiner組合器的描述,其他描述和前兩個方法無二。

The identity value must be an identity for the combiner function.his means that for all u, combiner(identity, u) is equal to code u. Additionally, the code combiner function must be compatible with thecode accumulator function; for all u and t。

大致意思:組合器需要和累加器的返回類型需要進行兼容,combiner組合器的方法主要用在并行操作中。如果你使用了parallelStream reduce操作是并發進行的 為了避免競爭 每個reduce線程都會有獨立的result combiner的作用在于合并每個線程的result得到最終結果

reduce方法運行效果:

U result = identity;

for (T element : this stream){

result = accumulator.apply(result, element)

}

return result;

與前兩個方法的不同點:

主要是初始值與用于遍歷流的對象類型不同,可以進行許多騷操作,例如ArrayList內添加數據,StringBulider拼接數據。

非并行:向ArrayList添加數據:

向arr集合后面添加num集合的數值,由于是非并行操作,combiner組合器方法沒有效果,只要參數與返回類型正確即可。

List num = Arrays.asList(1, 2, 3, 4, 5, 6);

ArrayList arr = new ArrayList<>();

arr.add(7);

arr.add(8);

arr.add(9);

arr.add(10);

List reduce = num.stream().reduce(arr, (x, y) -> {

x.add(y);

return x;

}, (List x, List y) -> {

System.out.println("并行才會出現");

return x;

});

System.out.println(reduce);

并行:集合內數據求和:

List num = Arrays.asList(1, 2, 3, 4, 5, 6);

Integer num1 = num.parallelStream().reduce(7, (x, y) -> x + y, (x, y)->{

System.out.println("這里調用一次");

return x + y;

});

System.out.println(num1);

123456

預算結果應該為1+…+7=28,然而結果為67,那這里應該是這樣的,調用6個線程進行計算,每個線程都以7作為初始值進行計算,最后將每個線程進行累計操作。combiner組合器的作用應該大致為將每個線程所計算的結果進行組合。

總結

以上是生活随笔為你收集整理的Java connot reduce_Java8: Reduce方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。