java 8 流map_使用map惯用的Java 8流?

这个问题已经在这里有了答案:            >            Java stream “forEach” but not consuming stream                                    1个

我真正喜欢的Ruby功能之一是ability to tap into call chains.它提供了一种调试管道中正在发生的事情的简便方法.我用地图模拟了水龙头:

/** 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 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

}

这实际上帮助我修复了一个错误,该错误是我在执行Path :: endsWith而不是String :: endsWith来查看文件名是否以特定扩展名结尾.

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

解决方法:

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

标签:java-8,java-stream,ruby,java

来源: https://codeday.me/bug/20191027/1943397.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值