java - 使用 Java 流同时在两个列表中添加数字
问题描述
我正在学习java中的流,我很困惑。我在谷歌上搜索了很多,但找不到方法。地图仍然让我感到困惑。
所以,我有两个相同大小的整数列表 PlayerA[] 和 PlayerB[] 8。我试图添加每个列表中除最后一个之外的所有元素。
int smallPitStonesPlayerA = 0;
int smallPitStonesPlayerB = 0;
for (int i = 1; i <= 6; i++) {
smallPitStonesPlayerA = smallPitStonesPlayerA + playerA.getStonesInPit(i);
smallPitStonesPlayerB = smallPitStonesPlayerB + playerB.getStonesInPit(i) ;
}
到目前为止我所尝试的。但这相当于两个循环。有没有办法做到这一点?
int num1 = playerA.stream()
.map(n-> n.getStonesInPit())
.collect(Collectors.summingInt(Integer::intValue));
int num2 = playerB.stream()
.map(n-> n.getStonesInPit())
.collect(Collectors.summingInt(Integer::intValue));
解决方案
推荐阅读
- java - 错误!ant 1.10.8 Java 8 和 XSLT 找不到类错误
- python - 如何在 tkinter 应用程序中推送更新
- python - Flask 如何在没有我将请求作为参数的情况下通过装饰器传递请求变量?
- javascript - 如何扩展正则表达式以接受图像网址中的@
- nginx - Nginx 支持带有回退的特定通配符 server_name (regex)
- angular - 在 html 中动态命名 div 并使用 @ViewChild('xyz') div: ElementRef; 在角度
- apache-kafka - 无法使用骆驼连接卡夫卡
- python-3.x - 在 Python 中迭代图像并转换为表 OCR 的 base64 格式
- r - blogdown::serve_site() 无法渲染 Rmd 文件
- vowpalwabbit - 当我们在 vw-hyperopt 中指定“--algorithms=sgd”时,它是否以自适应、标准化和不变的更新运行?