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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值