Path path = Paths.get("D:\\routeplanningbyweb\\springboot\\src\\test\\java\\com\\mongodb\\test.txt");
String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
System.out.println(contents);
Stream<String> words = Stream.of(contents.split("\\PL+"));
show("words", words);
Stream<String> songs = Stream.of("gently", "down", "the", "stream");
show("songs", songs);
Stream<String> silence = Stream.empty();
show("silence", silence);
Stream<String> echos = Stream.generate(()->"Echo");
show("echos", echos);
Stream<Double> randoms = Stream.generate(Math::random);
show("randoms", randoms);
Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE, n->n.add(BigInteger.ONE));
show("integers", integers);
Stream<String> wordsAnotherWay = Pattern.compile("\\PL+").splitAsStream(contents);
show("wordsAnotherWay", wordsAnotherWay);
try(Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)){
show("lines", lines);
}
Iterable<Path> iterable = FileSystems.getDefault().getRootDirectories();
Stream<Path> rootDirectories = StreamSupport.stream(iterable.spliterator(), false);
show("rootDirectories", rootDirectories);
Iterator<Path> iterator = Paths.get("./test.txt").iterator();
Stream<Path> pathComponents = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
show("pathComponents", pathComponents);
java入门之:产生stream的方法
最新推荐文章于 2023-07-13 11:32:44 发布