我有一个名为“目录”的ArrayList文章(泛型类型)。
文章有以下方法:
public int getUnitsInStore()
public long getUnitPrice()
为了获得目录中所有文章的总价值,我尝试使用Java流Api的Reduce方法。
我尝试了以下内容:
long value = 0;
value = catalog.stream().reduce((a,b) -> a.getUnitPrice()*b.getUnitsInStore());
但这给了我一个错误:
Type mismatch: cannot convert from Optional<Article> to long
我做错了什么?
我建议您使用流。reduce()
重载,需要两个参数:
因此,您将使用以下方式获得您的价值:
value = catalog
.stream()
.reduce(0L, (partialSum, nextElement) -> partialSum + nextElement.getUnitPrice() * nextElement.getUnitsInStore());
有三种类型的减少方法。尝试这段代码以更好地理解它
/**
* 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);
累加器lambda中的a
和b
分别是流中的部分结果和下一个元素。由于您正在减少文章列表,因此部分结果是文章,并且您不能将Long添加到文章中。
所以在你的情况下,你可能想做这样的事情。
value = catalog.stream()
.map(a -> a.getUnitPrice() * a.getUnitsInStore())
.reduce(0L, Long::sum);
我正在尝试创建一个查找器,它需要几个谓词并减少它们:
问题内容: 因此,我正在开发这个通用的HashTable类,并且希望将其通用地用于任何数量的传入类型,并且我还想将内部存储数组初始化为LinkedList的数组(出于冲突目的),其中为确保类型安全,每个LinkedList都被预先指定为HashTable类中的泛型类型。我该怎么做?以下代码最能阐明我的意图,但当然不会编译。 问题答案: Java中的泛型不允许创建具有泛型类型的数组。您可以将数组转换
问题内容: 我最近开始学习JAVA泛型。一切都说得通,我现在有点理解。但是有一件事让我烦恼-您无法创建通用类型的数组。 我想实现诸如队列和堆栈之类的抽象数据类型,但是要使用某种通用类型作为存储在堆栈中的基础数据。我将如何解决?我确定我想念但那是什么? 提前致谢。 问题答案: 《有效的Java》,第5章,GENERICS,第25项:首选列表而不是数组 : 数组在两个重要方面不同于通用类型。首先,数组
我不想为每个类型T编写这个方法只是为了调用getMessage()并将其传递给下一个方法。 有可能写出这样的方法吗?我只想访问ConstraintViolation接口的方法,这些方法不依赖于类型T(如字符串getMessage())。
问题内容: 我有一个方法以a 作为参数。 在中,我如何知道a 是还是a 是? 问题答案: 根据用户omain的回答“如果使用<?>,则意味着您将不会在任何地方使用参数化类型。要么转到特定类型(在您的情况下,似乎是),要么转到非常通用的“ 另外,我相信如果您使用问号,编译器将在运行时(类型;有效Java的第119页)消除类型不匹配的情况,绕过擦除,并有效地消除了使用泛型类型所带来的好处? 要回答发问
问题内容: 我正在尝试创建一个泛型类型的数组。我收到错误消息: 我很困惑。任何线索为什么会这样。 问题答案: 其背后的原因是,您不能创建通用或参数化类型的数组,而只能 创建可验证的 类型(即可以在运行时推断出的类型)。 尽管可以将此类数组类型 声明 为变量或方法参数。这有点不合逻辑,但这就是Java的样子。 Java泛型和集合在第6章中广泛讨论了此问题和相关问题。