java - 如何使用流和 lambda 从 int[] 数组中打印和选择数字
问题描述
我的目标是从数组中读取和选择数字,然后将大于 0 的数字打印出来,这是我的代码示例:
public class LambdaTest {
private int[] num = new int[10];
public static void main(String[] args) {
LambdaTest test = new LambdaTest();
//Lista de números a ordenar
test.num[0] = 5;
test.num[1] = -6;
test.num[2] = 7;
test.num[3] = 23;
test.num[4] = -1;
test.num[5] = 55;
test.num[6] = 78;
test.num[7] = 45;
test.num[8] = 31;
test.num[9] = -67;
//Proceso normal
for (int i=0; i<test.num.length; i++){
if (test.num[i]>=0){
System.out.println(test.num[i]);
}
}
}
}
但是对于 lambda 表达式和流,在此先感谢
解决方案
你可以做
Arrays.stream(test.num).filter(n -> n >= 0).forEach(System.out::println);
推荐阅读
- jenkins - Jenkins Ingress 与 k8s v1.19
- python - Python - 弹性搜索中的余弦相似度查询期间出现非法参数异常
- typescript -
和 React.createElement - ffmpeg - FFmpeg 和 Gstreamer:方法 SETUP 失败:461 不支持的传输
- node.js - Docker 映像无法在 Raspberry Pi 上运行,出现奇怪的节点错误
- docker - 从 gitlab ci 中的容器发送电子邮件
- reactjs - 有条件地将 TableCell 的全部内容变成链接
- javascript - Firefox 中的 Chart.js DOMException
- questdb - 如何将 QuestDB 从 6.1 降级到 6.0.9?
- python - 传递列表/数组作为 scikit 学习模型的输入