【问题标题】:How do I print and select numbers from an int[] array with streams and lambdas [closed]如何使用流和 lambda 从 int[] 数组中打印和选择数字 [关闭]
【发布时间】:2021-06-20 07:59:35
【问题描述】:

我的目标是从数组中读取和选择数字,然后取出大于 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 表达式和流,在此先感谢

【问题讨论】:

    标签: java arrays lambda integer


    【解决方案1】:

    你可以的

    Arrays.stream(test.num).filter(n -> n >= 0).forEach(System.out::println);
    

    【讨论】:

    • 就是这样!!如果你不介意我问的话,我有个问题。我在哪里可以学习如何正确使用 Lambdas 和 Streams,我看了很多视频并阅读了很多 PDF,但我无法正确掌握它。如果你能给我推荐一些东西,我将不胜感激,谢谢;)
    • 看来你有很多教程,但只需要在典型场景中练习......
    • 那我会继续努力的,谢谢!!
    猜你喜欢
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多