ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
### [跟踪和调试](https://lingcoder.gitee.io/onjava8/#/book/14-Streams?id=%e8%b7%9f%e8%b8%aa%e5%92%8c%e8%b0%83%e8%af%95) `peek()`操作的目的是帮助调试。它允许你无修改地查看流中的元素。代码示例: ~~~ // streams/Peeking.java class Peeking { public static void main(String[] args) throws Exception { FileToWords.stream("Cheese.dat") .skip(21) .limit(4) .map(w -> w + " ") .peek(System.out::print) .map(String::toUpperCase) .peek(System.out::print) .map(String::toLowerCase) .forEach(System.out::print); } } ~~~ 输出结果: ~~~ Well WELL well it IT it s S s so SO so ~~~ `FileToWords`稍后定义,但它的功能实现貌似和之前我们看到的差不多:产生字符串对象的流。之后在其通过管道时调用`peek()`进行处理。 因为`peek()`符合无返回值的**Consumer**函数式接口,所以我们只能观察,无法使用不同的元素来替换流中的对象。