【发布时间】:2018-10-17 18:40:35
【问题描述】:
我正在尝试从文件夹路径作为参数传入的文件夹中读取图像,我正在尝试使用流 API 将它们添加到数组列表中,但我不知道我应该使用什么变量在 forEach() 中添加到我的数组列表中
public void downsampling(String inputPath, String outputPath) {
ArrayList<BufferedImage> inputSequence = new ArrayList<BufferedImage>();
try (Stream<Path> paths = Files.walk(Paths.get(inputPath))) {
paths
.forEach(inputSequence.add(XXX));
} catch (IOException e) {
e.printStackTrace();
}
}
XXX 应该换成什么?非常感谢
【问题讨论】:
-
.forEach(x -> inputSequence.add(x)); -
工作得很好,谢谢!
-
最好使用
inputSequence = paths.map(…).collect(Collectors.toList());但是为什么要将这些图像收集到List中呢?看来,您的方法应该处理它们,而不是返回它们,所以只需一个一个处理它们,而不将它们添加到List。 -
嗯,是的,我确实可以在不将它们添加到列表的情况下处理它们,但这是当时想到的,但它仍然有效,谢谢你接下来我会尝试使用你的建议时间。
标签: java arrays arraylist stream java-stream