编程语言
首页 > 编程语言> > Java-IntStream

Java-IntStream

作者:互联网

目录

官方文档

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

iterate

public class Tester {
    public static void main(String[] args) {
        // x小于20一直打印偶数
        IntStream.iterate(0, x -> x < 20, x -> x + 2).forEach(System.out::println);
        // 一直打印
        IntStream.iterate(0, Tester::applyAsInt).forEach(System.out::println);
    }

    private static int applyAsInt(int x) {
        try {
            Thread.sleep(1000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return x + 2;
    }
}

limit

public class Tester {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
        intStream.limit(3).forEach(System.out::println);
    }
}
1
2
3

range

public class Tester {
    public static void main(String[] args) {
    	// 不包含3
        IntStream.range(1, 3).forEach(System.out::println);
        // 包含3
        IntStream.rangeClosed(1, 3).forEach(System.out::println);
    }
}
1
2
1
2
3


takeWhile

public class Tester {
    public static void main(String[] args) {
        // 从Stream中依次获取满足条件的元素,直到不满足条件为止结束获取
        IntStream.of(7, 8, 1, 2, 3).takeWhile(i -> i > 4).forEach(System.out::print);
        System.out.println();
        IntStream.of(12, 4, 3, 6, 8, 9).takeWhile(x -> x % 2 == 0).forEach(System.out::print);
        System.out.println();
    }
}
78
124

dropWhile

public class Tester {
    public static void main(String[] args) {
        // 从Stream中依次删除满足条件的元素,直到不满足条件为止结束删除
        IntStream.of(12, 4, 3, 6, 8, 9).dropWhile(x -> x % 2 == 0).forEach(System.out::print);
        System.out.println();
        IntStream.of(7, 8, 1, 2, 3).dropWhile(i -> i > 4).forEach(System.out::print);
    }
}
3689
123

filter

获取流中符合filter过滤函数的元素

public class Tester {
    public static void main(String[] args) {
        IntStream.of(12, 4, 3, 6, 8, 9).filter(i -> i %2 ==0).forEach(System.out::println);
    }
}
12
4
6
8

average

求元素的平均数

public class Tester {
    public static void main(String[] args) {
        IntStream.of(12, 4, 3, 6, 8, 9).filter(i -> i %2 ==0).forEach(System.out::println);
        System.out.println(IntStream.of(1, 2, 3, 4).average());
    }
}

OptionalDouble[2.5]

distinct

public class Tester {
    public static void main(String[] args) {
        IntStream.of(1, 2, 3, 4, 1, 2, 5, 8).distinct().forEach(System.out::println);
    }
}
123458

peek

public class Tester {
    public static void main(String[] args) {
        IntStream.of(1, 2, 3, 4)
                .filter(e -> e > 2)
                .peek(e -> System.out.println("Filtered value: " + e))
                .map(e -> e * e)
                .peek(e -> System.out.println("Mapped value: " + e))
                .sum();

    }
}
Filtered value: 3
Mapped value: 9
Filtered value: 4
Mapped value: 16

laziness

public class Tester {
    public static void main(String[] args) throws InterruptedException {

        List<Integer> list = List.of(1, 2);
        Stream<Integer> streamOfNames = list.stream()
                .map(i -> {
                    System.out.println("In Map - " + i);
                    return i;
                });

        for (int i = 1; i <= 3; i++) {
            Thread.sleep(1000);
            System.out.println(i + " sec");
        }

        streamOfNames.collect(Collectors.toList());
    }
}
1 sec
2 sec
3 sec
In Map - 1
In Map - 2

标签:Java,System,forEach,println,IntStream,public,out
来源: https://blog.csdn.net/weixin_38788159/article/details/115396998