streams - Regular Expressions

Java 8 added a new method splitAsStream() to the java.util.regex.Pattern class, which takes a sequence of characters and splits it into a stream, according to the formula we hand it. There’s a constraint, which is that the input is a CharSequence , so we cannot feed a stream into splitAsStream().

// streams/FileToWordsRegexp.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.io.*;
import java.nio.file.*;
import java.util.regex.Pattern;
import java.util.stream.*;

public class FileToWordsRegexp {
  private String all;

  public FileToWordsRegexp(String filePath) throws Exception {
    all =
        Files.lines(Paths.get(filePath))
            .skip(1) // First (comment) line
            .collect(Collectors.joining(" "));
  }

  public Stream<String> stream() {
    return Pattern.compile("[ .,?]+").splitAsStream(all);
  }

  public static void main(String[] args) throws Exception {
    FileToWordsRegexp fw = new FileToWordsRegexp("Cheese.dat");
    fw.stream().limit(7).map(w -> w + " ").forEach(System.out::print);
    fw.stream().skip(7).limit(2).map(w -> w + " ").forEach(System.out::print); // map() method return the new stream
  }
}
/* Output:
Not much of a cheese shop really is it
*/

The limit here is that the whole file must be stored in memory; most of the time that probably won’t be an issue but it loses important benefits of streams:

  1. They “don’t require storage.” Of course they actually require some internal storage, but it’s only a fraction of the sequence, and nothing like what it takes to hold the entire sequence.
  2. They are lazily evaluated.

the solution is here - FileToWord by using flatMap() method.

see previoue example.

references:

1. On Java 8 - Bruce Eckel

2. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#splitAsStream-java.lang.CharSequence-

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/Cheese.dat

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/FileToWordsRegexp.java

5. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#limit-long-

6. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map-java.util.function.Function-

7. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#skip-long-

8. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值