CircularFifoBuffer .avg() 计算失败
我有以下虚拟代码,它将 hasmap 中的双精度列表保存到特定键,我希望能够使用此缓冲区来计算每个键的平均值。
final int bufferSize = 5;
HashMap<Integer, List<Double>> testmap = new HashMap<Integer, List<Double>>();
List<Double> Values1 = Arrays.asList(1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9);
List<Double> Values2 = Arrays.asList(22.2,33.3,44.4,55.5,66.6,77.7,88.8,99.9);
List<Double> Values3 = Arrays.asList(333.3,444.4,555.5,666.6);
testmap.put(123456, Values1);
testmap.put(234567, Values2);
testmap.put(345678, Values3);
HashMap<Integer, CircularFifoBuffer> buffer = new HashMap<Integer, CircularFifoBuffer>();
for (Map.Entry<Integer, List<Double>> entry: testmap.entrySet()) {
Integer key = entry.getKey();
List<Double> value = entry.getValue();
CircularFifoBuffer bufferEntries = new CircularFifoBuffer(4);
for(Double val : value){
bufferEntries.add(val);
}
buffer.put(key, bufferEntries);
}
我现在想计算一个简单的平均值
buffer.get(345678).stream().mapToDouble(d -> d).average().orElse(0.0)
,它返回“不兼容的类型:lambda 表达式中的返回类型错误,无法转换为 Double。我尝试不使用 maptoDOuble,但后来我没有得到 avg() 方法。任何人都知道为什么会赢不工作
I have the following dummy code which saves a list of doubles in a hasmap to a specific key, I want to be able to use this buffer to calculate average values over each key.
final int bufferSize = 5;
HashMap<Integer, List<Double>> testmap = new HashMap<Integer, List<Double>>();
List<Double> Values1 = Arrays.asList(1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9);
List<Double> Values2 = Arrays.asList(22.2,33.3,44.4,55.5,66.6,77.7,88.8,99.9);
List<Double> Values3 = Arrays.asList(333.3,444.4,555.5,666.6);
testmap.put(123456, Values1);
testmap.put(234567, Values2);
testmap.put(345678, Values3);
HashMap<Integer, CircularFifoBuffer> buffer = new HashMap<Integer, CircularFifoBuffer>();
for (Map.Entry<Integer, List<Double>> entry: testmap.entrySet()) {
Integer key = entry.getKey();
List<Double> value = entry.getValue();
CircularFifoBuffer bufferEntries = new CircularFifoBuffer(4);
for(Double val : value){
bufferEntries.add(val);
}
buffer.put(key, bufferEntries);
}
I now want to calculate a simple average
buffer.get(345678).stream().mapToDouble(d -> d).average().orElse(0.0)
Which returns "incompatible types: bad return type in lambda expression, cannot be converted to Double. I tried without the maptoDOuble but then I do not get the avg() method. ANyone any idea why this won't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CircularFifoBuffer 不是通用的,它使用固定的
Object
类型:然后,您必须指定确切的源类型,因为
Object
可以是任何类型。如果您编写代码进行编译并生成预期的输出,
那么Java 泛型基础知识。
CircularFifoBuffer is not generic, it use the fixed
Object
type:then, you must to specify the exact source type since
Object
could be any type. If you writeyour code compile and produce the expected output
a good reading might be The Basics of Java Generics.