Java 8 Streams. Pass Two arguments to a method called in forEach

问题: Using Java 8 streams API, I want a way to call a reference method which accepts two arguments. splitFileByMaxRows is the reference method that should take a String and an i...

问题:

Using Java 8 streams API, I want a way to call a reference method which accepts two arguments. splitFileByMaxRows is the reference method that should take a String and an int as its arguments. Is there any way to achieve it?

private void breakLargeFileIntoChunks(final File setlFile, int parentFileId) {
    LOG.info(LOG.isInfoEnabled() ? "*** Breaking Large File Into Chunks ***" : null);

    try (Chunker chunker = new Chunker(); 
         Stream<String> lines = Files.lines(Paths.get(setlFile.getAbsolutePath()))) {
        lines.forEach(chunker::splitFileByMaxRows);
    }
    catch (IOException e) {
        e.printStackTrace();
    }

}

回答1:

You can't use a method reference, since you have no way of passing the int argument to it.

Therefore, use a lambda expression instead:

lines.forEach(s -> chunker.splitFileByMaxRows(s,someInt));
  • 发表于 2018-07-10 16:43
  • 阅读 ( 235 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除