1 重点
1.1 流的构建的四种方式
2 Demo之由数值构建流
demo:
@Test /** * 1 由数值直接构建流 * 问题:为什么可以用System.out::println ? */ public void streamFromValue(){ Stream stream = Stream.of(1,2,3,4,5); stream.forEach(System.out::println); }
打印日志:
1
2
3
4
5
Process finished with exit code 0
3 Demo之由数组构建流:
demo:
@Test /** * 2 通过数组构建流 */ public void streamFromArray(){ int[] intValue = {1,2,3,4,5,6}; IntStream intStream = Arrays.stream(intValue); intStream.forEach(System.out::println); }
打印日志:
1
2
3
4
5
6
Process finished with exit code 0
4 Demo之由文件构建流:
demo:
/** * 3 通过文件生成流 * 重点:可以看看Files.lines 里面的内容是如何生成的(为什么用Paths.get获取到path,主要是点进去方法,Path接口中 是可以通过Paths.get方法得到) */ @Test public void streamFromFile() throws IOException { Stream<String> stream = Files.lines(Paths.get("F:\\xiangmu3\\Xin\\test996\\src\\test\\java\\com\\imooc\\zhangxiaoxi\\stream\\StreamConstructor.java")); stream.forEach(System.out::println); }
打印日志:
D:\java\jdk\jdk9\jdk-9+181_windows-x64_ri\java-se-9-ri\jdk-9\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=14554:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit-rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit5-rt.jar;F:\xiangmu3\Xin\test996\target\test-classes;F:\xiangmu3\Xin\test996\target\classes;F:\xiangmu3\Xin\test996\lib\com\google\guava\guava\28.2-jre\guava-28.2-jre.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:\xiangmu3\Xin\test996\lib\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;F:\xiangmu3\Xin\test996\lib\org\checkerframework\checker-qual\2.10.0\checker-qual-2.10.0.jar;F:\xiangmu3\Xin\test996\lib\com\google\errorprone\error_prone_annotations\2.3.4\error_prone_annotations-2.3.4.jar;F:\xiangmu3\Xin\test996\lib\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;F:\xiangmu3\Xin\test996\lib\junit\junit\4.12\junit-4.12.jar;F:\xiangmu3\Xin\test996\lib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\xiangmu3\Xin\test996\lib\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit3 com.imooc.zhangxiaoxi.stream.StreamConstructor,streamFromFile package com.imooc.zhangxiaoxi.stream; import org.junit.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.Iterator; import java.util.stream.IntStream; import java.util.stream.Stream; /** * StreamConstructor * 用来演示流的四种构建形式 * @author 魏豆豆 * @date 2020/4/30 */ public class StreamConstructor { @Test /** * 1 由数值直接构建流 * 问题:为什么可以用System.out::println ? */ public void streamFromValue(){ Stream stream = Stream.of(1,2,3,4,5); stream.forEach(System.out::println); } @Test /** * 2 通过数组构建流 */ public void streamFromArray(){ int[] intValue = {1,2,3,4,5,6}; IntStream intStream = Arrays.stream(intValue); intStream.forEach(System.out::println); } /** * 3 通过文件生成流 * 重点:可以看看Files.lines 里面的内容是如何生成的(为什么用Paths.get获取到path,主要是点进去方法,Path接口中 是可以通过Paths.get方法得到) */ @Test public void streamFromFile() throws IOException { Stream<String> stream = Files.lines(Paths.get("F:\\xiangmu3\\Xin\\test996\\src\\test\\java\\com\\imooc\\zhangxiaoxi\\stream\\StreamConstructor.java")); stream.forEach(System.out::println); } /** * 4 通过函数生成流 * 生成3的等差数列,取前10条 * 生成5个随机数 * 问题:为什么Stream.generate(Math.radom()) 方法不可以呢? */ @Test public void streamFromFunction(){ /*Stream stream = Stream.iterate(0,i->i+3).limit(10); stream.forEach(System.out::println);*/ Stream stream = Stream.generate(Math::random); stream.limit(5).forEach(System.out::println); } } Process finished with exit code 0
5 Demo之由函数构建流:
demo:
/** * 4 通过函数生成流 * 生成3的等差数列,取前10条 * 生成5个随机数 * 问题:为什么Stream.generate(Math.radom()) 方法不可以呢? */ @Test public void streamFromFunction(){ /*Stream stream = Stream.iterate(0,i->i+3).limit(10); stream.forEach(System.out::println);*/ Stream stream = Stream.generate(Math::random); stream.limit(5).forEach(System.out::println); }
打印日志:
0.10992075250417033
0.4290787589469168
0.20679958417543365
0.04201955124184875
0.43265370281686455
Process finished with exit code 0
诸葛