Java 8 Streams.将两个参数传递给forEach中调用的方法

mal*_*are 1 java java-8 java-stream

使用Java 8流API,我想要一种方法来调用一个接受两个参数的引用方法.splitFileByMaxRows是一个应该采用a String和a int作为参数的引用方法.有没有办法实现它?

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 6

您不能使用方法引用,因为您无法将int参数传递给它.

因此,请改用lambda表达式:

lines.forEach(s -> chunker.splitFileByMaxRows(s,someInt));
Run Code Online (Sandbox Code Playgroud)