Java connot reduce_Java8: Reduce方法
一:reduce
rudece方法:從一個(gè)流中生成一個(gè)值
三個(gè)重載方法:
Optional reduce(BinaryOperator accumulator);
T reduce(T identity, BinaryOperator accumulator);
U reduce(U identity,
BiFunction accumulator,
BinaryOperator combiner);
二:重載方法原理
一個(gè)參數(shù)
接口繼承詳情:
Optional reduce(BinaryOperator accumulator);
@FunctionalInterface
public interface BinaryOperator extends BiFunction {
//兩個(gè)靜態(tài)方法,先進(jìn)行忽略
}
@FunctionalInterface
public interface BiFunction {
R apply(T t, U u);
//一個(gè)默認(rèn)方法,先進(jìn)行忽略
}
這里可以看出,reduce方法參數(shù)為一個(gè)函數(shù),返回值為Optional對象,BinaryOperator的作用為規(guī)定BiFunction的三個(gè)參數(shù)泛型類型要一致,也就是說只要我們對apply方法進(jìn)行實(shí)現(xiàn)并傳進(jìn)去就ok了。
文檔中寫到:
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value。
大致意思:使用累積函數(shù)對此流的元素執(zhí)行操作,并返回一個(gè)描述結(jié)果的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類型對象對流進(jìn)行遍歷,第一個(gè)數(shù)值進(jìn)行賦值,其余apply操作,并將操作的結(jié)果進(jìn)行返回,等待下次繼續(xù)當(dāng)apply方法參數(shù)輸入。
那也就是說,我們可以這樣:
求和效果展示:
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);
//你也可以這樣寫,效果一樣,一個(gè)為Lambda表達(dá)式,一個(gè)匿名內(nèi)部類
Integer integer = num.stream().reduce(new BinaryOperator() {
@Override
public Integer apply(Integer a, Integer b) {
return a + b;
}
}).get();
兩個(gè)參數(shù)
接口繼承詳情:
該方法的參數(shù)多了一個(gè)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.
大致意思:使用提供的初始值和累計(jì)函數(shù)對流進(jìn)行操作,并返回一個(gè)初始值類型的計(jì)算結(jié)果。
T result = identity;
for (T element : this stream){
result = accumulator.apply(result, element)
}
return result;
reduce方法效果:使用identity作為初始值,每遍歷到一個(gè)數(shù)據(jù),用apply方法操作并將結(jié)果返回。
計(jì)和效果展示:
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);
不同點(diǎn):①,reduce中多了一個(gè)參數(shù),初始值identity。②方法的返回結(jié)果為初始值identity類型的對象。
三個(gè)參數(shù)
接口繼承詳情:
U reduce(U identity,
BiFunction accumulator,
BinaryOperator combiner);
@FunctionalInterface
public interface BiFunction {
R apply(T t, U u);
//一個(gè)默認(rèn)方法,忽略
}
這里先看一個(gè)reduce的參數(shù):①,U類型的初始值。②,(T,U)→U,T+U返回U類型。③組合器,(T,T)→T,T+T返回T類型。
文檔描述:
這個(gè)是對于combiner組合器的描述,其他描述和前兩個(gè)方法無二。
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。
大致意思:組合器需要和累加器的返回類型需要進(jìn)行兼容,combiner組合器的方法主要用在并行操作中。如果你使用了parallelStream reduce操作是并發(fā)進(jìn)行的 為了避免競爭 每個(gè)reduce線程都會有獨(dú)立的result combiner的作用在于合并每個(gè)線程的result得到最終結(jié)果
reduce方法運(yùn)行效果:
U result = identity;
for (T element : this stream){
result = accumulator.apply(result, element)
}
return result;
與前兩個(gè)方法的不同點(diǎn):
主要是初始值與用于遍歷流的對象類型不同,可以進(jìn)行許多騷操作,例如ArrayList內(nèi)添加數(shù)據(jù),StringBulider拼接數(shù)據(jù)。
非并行:向ArrayList添加數(shù)據(jù):
向arr集合后面添加num集合的數(shù)值,由于是非并行操作,combiner組合器方法沒有效果,只要參數(shù)與返回類型正確即可。
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("并行才會出現(xiàn)");
return x;
});
System.out.println(reduce);
并行:集合內(nèi)數(shù)據(jù)求和:
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("這里調(diào)用一次");
return x + y;
});
System.out.println(num1);
123456
預(yù)算結(jié)果應(yīng)該為1+…+7=28,然而結(jié)果為67,那這里應(yīng)該是這樣的,調(diào)用6個(gè)線程進(jìn)行計(jì)算,每個(gè)線程都以7作為初始值進(jìn)行計(jì)算,最后將每個(gè)線程進(jìn)行累計(jì)操作。combiner組合器的作用應(yīng)該大致為將每個(gè)線程所計(jì)算的結(jié)果進(jìn)行組合。
總結(jié)
以上是生活随笔為你收集整理的Java connot reduce_Java8: Reduce方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10款好看且实用的文字动画特效,让你的页
- 下一篇: java怎么设置快速修复键_Java开发