使用map习惯性地攻入Java8流?

使用map习惯性地攻入Java8流?,java,ruby,java-8,java-stream,Java,Ruby,Java 8,Java Stream,我非常喜欢Ruby的一个特性是。它提供了一种调试管道中正在发生的事情的简单方法。我用地图模拟了点击: /** Searches recursively and returns the path to the dir that has a file with given extension, * null otherwise. * Returns the given dir if it has a file with given extension. * @param dir Path t

我非常喜欢Ruby的一个特性是。它提供了一种调试管道中正在发生的事情的简单方法。我用
地图模拟了
点击

/** Searches recursively and returns the path to the dir that has a file with given extension,
 *  null otherwise.
 * Returns the given dir if it has a file with given extension.
 * @param dir Path to the start folder
 * @param ext String denotes the traditional extension of a file, e.g. "*.gz"
 * @return {@linkplain Path} of the folder containing such a file, null otherwise
 */
static Path getFolderWithFilesHavingExtension(Path dir, String ext) {
    Objects.requireNonNull(dir); // ignore the return value
    Objects.requireNonNull(ext); // ignore the return value
    try {
        Optional<Path> op = Files.walk(dir, 10).map((t) -> {
            System.out.println("tap: " + t.endsWith(ext));
            System.out.println("tap: " + t.toString().endsWith(ext));
            return t;
        }).filter(p -> p.toString().endsWith(ext)).limit(1).findFirst();
        if (op.isPresent())
            return op.get().getParent();
    } catch (IOException e) {
        return null; // squelching the exception is okay? //TODO
    }
    return null; // no such files found
}
/**递归搜索并返回具有给定扩展名的文件的目录路径,
*否则为空。
*如果文件具有给定的扩展名,则返回给定的目录。
*@param dir指向开始文件夹的路径
*@param ext String表示文件的传统扩展名,例如“*.gz”
*包含此文件的文件夹的@return{@linkplain Path},否则为null
*/
静态路径getFolderWithFilesHavingExtension(路径目录,字符串扩展名){
Objects.requirennull(dir);//忽略返回值
Objects.requirennull(ext);//忽略返回值
试一试{
可选op=Files.walk(dir,10).map((t)->{
System.out.println(“点击:+t.endsWith(ext));
System.out.println(“点击:+t.toString().endsWith(ext));
返回t;
}).filter(p->p.toString().endsWith(ext)).limit(1.findFirst();
if(op.isPresent())
返回op.get().getParent();
}捕获(IOE异常){
返回null;//对异常进行静噪可以吗?//TODO
}
return null;//找不到这样的文件
}
这实际上帮助我修复了一个错误,因为我正在执行
Path::endsWith
而不是
String::endsWith
以查看文件名是否以特定扩展名结尾

在Java 8中有更好的(惯用的)方法吗?

您可以使用
.peek(System.out::println)
.peek(t->“点击:”+t.endsWith(ext))