方式 | 方法 | 示例 |
---|---|---|
由值创建流 | Stream.of | Stream<String> stream = Stream.of("Java 8 ","Lambdas ", "In ", "Action"); stream.map(String::toUpperCase).forEach(System.out::println); |
由数组创建流 | Arrays.stream | int[] numbers = {2, 3, 5, 7, 11, 13}; int sum = Arrays.stream(numbers).sum(); (值为41) |
由文件生成流 | Files.lines | Stream<String> lines = Files.lines(Paths.get("/Users/user/Downloads/2019-04-18.csv") |
创建无限流 | Stream.iterate、 Stream.generate | Stream.iterate(0, n -> n + 2).limit(10) .forEach(System.out::println); 、Stream.generate(Math::random) .limit(5).forEach(System.out::println); |
创建Stream
最新推荐文章于 2024-05-28 17:26:38 发布