• Java8-Stream-No.04


    import java.util.OptionalInt;
    import java.util.stream.IntStream;
    
    public class Streams4 {
    
        public static void main(String[] args) {
            for (int i = 0; i < 10; i++) {
                if (i % 2 == 1) {
                    System.out.println(i);
                }
            }
    
            IntStream.range(0, 10)
                .forEach(i -> {
                    if (i % 2 == 1) System.out.println(i);
                });
    
            IntStream.range(0, 10)
                .filter(i -> i % 2 == 1)
                .forEach(System.out::println);
    
            OptionalInt reduced1 =
                IntStream.range(0, 10)
                    .reduce((a, b) -> a + b);
            System.out.println(reduced1.getAsInt());
    
            int reduced2 =
                IntStream.range(0, 10)
                    .reduce(7, (a, b) -> a + b);
            System.out.println(reduced2);
        }
    }
    
  • 相关阅读:
    汉罗塔
    汉罗塔动画实现
    画五星红旗
    科学与可视化
    类和正则表达式
    圆周率
    汉诺塔
    jieba库
    Python图片处理
    随手笔记
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210934.html
Copyright © 2020-2023  润新知