Java Stream reduce泛型类型的ArrayList
我不太明白Java Stream Reduce是如何工作的 我有一个名为“Catalog”的ArrayList文章(泛型类型) 文章有以下几种方法:Java Stream reduce泛型类型的ArrayList,java,java-stream,reduce,Java,Java Stream,Reduce,我不太明白Java Stream Reduce是如何工作的 我有一个名为“Catalog”的ArrayList文章(泛型类型) 文章有以下几种方法: public int getUnitsInStore() public long getUnitPrice() 为了获得目录中所有文章的总价值,我尝试使用JavaStreamAPI的Reduce方法。 (请不要提供使用循环的anwser,我已经知道怎么做了) 我尝试了以下几点: long value = 0; value = catal
public int getUnitsInStore()
public long getUnitPrice()
为了获得目录中所有文章的总价值,我尝试使用JavaStreamAPI的Reduce方法。
(请不要提供使用循环的anwser,我已经知道怎么做了)
我尝试了以下几点:
long value = 0;
value = catalog.stream().reduce((a,b) -> a.getUnitPrice()*b.getUnitsInStore());
但这给了我一个错误:
Type mismatch: cannot convert from Optional<Article> to long
类型不匹配:无法从可选转换为长
我做错了什么?正如我所说,我不确定我是否真正理解reduce方法的工作原理。我建议您使用重载,它需要两个参数:
- identity:identity元素既是缩减的初始值,也是流中没有元素时的默认结果
- 累加器:累加器函数,包含两个参数:还原的部分结果和流的下一个元素。它返回一个新的部分结果
value=catalog
.stream()
.reduce(0L,(partialSum,nextElement)->partialSum+nextElement.getUnitPrice()*nextElement.getUnitsInStore());
我建议您使用重载,它需要两个参数:
- identity:identity元素既是缩减的初始值,也是流中没有元素时的默认结果
- 累加器:累加器函数,包含两个参数:还原的部分结果和流的下一个元素。它返回一个新的部分结果
value=catalog
.stream()
.reduce(0L,(partialSum,nextElement)->partialSum+nextElement.getUnitPrice()*nextElement.getUnitsInStore());
有三种类型的reduce方法。
尝试此代码以更好地理解它
/**
* T reduce(T identity, BinaryOperator<T> accumulator)
*
* identity = initial value
* accumulator = first process initial value to first element of the stream,
* then process the result with the next element in the stream
*/
String name = Stream.of("T", "O", "M", "M", "Y")
.reduce("", (a,n) -> a + n);
System.out.println(name);
int sum = Stream.of(1,2,3,4,5,6,7,8,9,10)
.reduce(0, (a,n) -> a + n);
System.out.println(sum);
int multi = Stream.of(1,2,3,4,5,6,7,8,9,10)
.reduce(1, (a,n) -> a * n);
System.out.println(multi);
/**
* Optional<T> reduce(BinaryOperator<T> accumulator)
*
*/
Optional<String> optName = Stream.of("T", "O", "M", "M", "Y")
.reduce((a,n) -> a + n);
if(optName.isPresent()){
System.out.println(" get from optional --> " + optName.get());
}
/**
* <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
*
* This method signature is used when we are dealing with different types.
* It allows Java to create intermediate reductions and then combine them at the end.
*/
int total = Stream.of("R", "a", "z", "v", "a", "n")
.reduce(0, (a,b) -> a + b.length(), (x,y) -> x + y);
// 0 = initial value type int
// a = int, must match the identity type
// b.method() return type must match the a and the identity type
// x,y from BinaryOperator
System.out.println(total);
String names[] = {"Bobby", "Mark", "Anthony", "Danna"};
int sumAll = Stream.of(names)
.reduce(0, (a,b) -> a + b.length(), (x,y) -> x + y);
System.out.println(sumAll);
String csvNames = Stream.of(names)
.reduce("", (a,b) -> a + b + ";");
System.out.println(csvNames);
/**
*T reduce(T标识,二进制运算符累加器)
*
*标识=初始值
*累加器=流的第一个元素的第一个进程初始值,
*然后使用流中的下一个元素处理结果
*/
字符串名称=Stream.of(“T”、“O”、“M”、“M”、“Y”)
.减少(“,(a,n)->a+n);
System.out.println(名称);
int sum=流(1,2,3,4,5,6,7,8,9,10)
.减少(0,(a,n)->a+n);
系统输出打印项数(总和);
int multi=流(1,2,3,4,5,6,7,8,9,10)
.减少(1,(a,n)->a*n);
系统输出打印项次(多);
/**
*可选减少(二进制运算符累加器)
*
*/
可选optName=Stream.of(“T”、“O”、“M”、“M”、“Y”)
.减少((a,n)->a+n);
if(optName.isPresent()){
System.out.println(“从可选-->获取”+optName.get());
}
/**
*U减少(U标识、双功能累加器、二进制运算符组合器)
*
*当我们处理不同的类型时,会使用此方法签名。
*它允许Java创建中间缩减,然后在最后合并它们。
*/
int total=流量(“R”、“a”、“z”、“v”、“a”、“n”)
.减少(0,(a,b)->a+b.length(),(x,y)->x+y);
//0=初始值类型int
//a=int,必须与标识类型匹配
//b.method()返回类型必须与a和标识类型匹配
//来自二进制运算符的x,y
系统输出打印项次(总计);
字符串名[]={“Bobby”、“Mark”、“Anthony”、“Danna”};
int sumAll=Stream.of(名称)
.减少(0,(a,b)->a+b.length(),(x,y)->x+y);
系统输出打印项次(sumAll);
字符串csvNames=Stream.of(名称)
.减少(“,(a,b)->a+b+”;”;
System.out.println(csvNames);
有三种类型的reduce方法。
尝试此代码以更好地理解它
/**
* T reduce(T identity, BinaryOperator<T> accumulator)
*
* identity = initial value
* accumulator = first process initial value to first element of the stream,
* then process the result with the next element in the stream
*/
String name = Stream.of("T", "O", "M", "M", "Y")
.reduce("", (a,n) -> a + n);
System.out.println(name);
int sum = Stream.of(1,2,3,4,5,6,7,8,9,10)
.reduce(0, (a,n) -> a + n);
System.out.println(sum);
int multi = Stream.of(1,2,3,4,5,6,7,8,9,10)
.reduce(1, (a,n) -> a * n);
System.out.println(multi);
/**
* Optional<T> reduce(BinaryOperator<T> accumulator)
*
*/
Optional<String> optName = Stream.of("T", "O", "M", "M", "Y")
.reduce((a,n) -> a + n);
if(optName.isPresent()){
System.out.println(" get from optional --> " + optName.get());
}
/**
* <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
*
* This method signature is used when we are dealing with different types.
* It allows Java to create intermediate reductions and then combine them at the end.
*/
int total = Stream.of("R", "a", "z", "v", "a", "n")
.reduce(0, (a,b) -> a + b.length(), (x,y) -> x + y);
// 0 = initial value type int
// a = int, must match the identity type
// b.method() return type must match the a and the identity type
// x,y from BinaryOperator
System.out.println(total);
String names[] = {"Bobby", "Mark", "Anthony", "Danna"};
int sumAll = Stream.of(names)
.reduce(0, (a,b) -> a + b.length(), (x,y) -> x + y);
System.out.println(sumAll);
String csvNames = Stream.of(names)
.reduce("", (a,b) -> a + b + ";");
System.out.println(csvNames);
/**
*T reduce(T标识,二进制运算符累加器)
*
*标识=初始值
*累加器=流的第一个元素的第一个进程初始值,
*然后使用流中的下一个元素处理结果
*/
字符串名称=Stream.of(“T”、“O”、“M”、“M”、“Y”)
.减少(“,(a,n)->a+n);
System.out.println(名称);
int sum=流(1,2,3,4,5,6,7,8,9,10)
.减少(0,(a,n)->a+n);
系统输出打印项数(总和);
int multi=流(1,2,3,4,5,6,7,8,9,10)
.减少(1,(a,n)->a*n);
系统输出打印项次(多);
/**
*可选减少(二进制运算符累加器)
*
*/
可选optName=Stream.of(“T”、“O”、“M”、“M”、“Y”)
.减少((a,n)->a+n);
if(optName.isPresent()){
System.out.println(“从可选-->获取”+optName.get());
}
/**
*U减少(U标识、双功能累加器、二进制运算符组合器)
*
*当我们处理不同的类型时,会使用此方法签名。
*它允许Java创建中间缩减,然后在最后合并它们。
*/
int total=流量(“R”、“a”、“z”、“v”、“a”、“n”)
.减少(0,(a,b)->a+b.length(),(x,y)->x+y);
//0=初始值类型int
//a=int,必须与标识类型匹配
//b.method()返回类型必须与a和标识类型匹配
//来自二进制运算符的x,y
系统输出打印项次(总计);
字符串名[]={“Bobby”、“Mark”、“Anthony”、“Danna”};
int sumAll=Stream.of(名称)
.减少(0,(a,b)->a+b.length(),(x,y)->x+y);
系统输出打印项次(sumAll);
字符串csvNames=Stream.of(名称)
.减少(“,(a,b)->a+b+”;”;
System.out.println(csvNames);
累加器lambda中的a
和b
分别是,