如何将计数器插入Stream <String> .forEach()?

Veg*_*ega 24 java foreach line-numbers java-stream

FileWriter writer = new FileWriter(output_file);
    int i = 0;

    try (Stream<String> lines = Files.lines(Paths.get(input_file))) {
        lines.forEach(line -> {
            try {
                writer.write(i + " # " + line + System.lineSeparator());
            } catch (Exception e) {
                e.printStackTrace();
            }   
        }
                    );
        writer.close();
    }
Run Code Online (Sandbox Code Playgroud)

我需要用行号写行,所以我试着在.forEach()中添加一个计数器,但是我无法让它工作.我只是不知道把i ++放在哪里; 进入代码,随机搞砸到目前为止没有帮助.

Old*_*eon 47

您可以使用AtomicInteger一个可变final计数器.

public void test() throws IOException {
    // Make sure the writer closes.
    try (FileWriter writer = new FileWriter("OutFile.txt") ) {
        // Use AtomicInteger as a mutable line count.
        final AtomicInteger count = new AtomicInteger();
        // Make sure the stream closes.
        try (Stream<String> lines = Files.lines(Paths.get("InFile.txt"))) {
            lines.forEach(line -> {
                        try {
                            // Annotate with line number.
                            writer.write(count.incrementAndGet() + " # " + line + System.lineSeparator());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*aas 15

这是一个很好的例子,你应该使用一个好的老式for循环.虽然Files.lines()专门给出了顺序流,但是流可以不按顺序生成和处理,因此插入计数器并依赖它们的顺序是一个相当不好的习惯.如果您仍然真的想要这样做,请记住,在任何可以使用lambda的地方,您仍然可以使用完整的匿名类.匿名类是普通类,因此可以具有状态.

所以在你的例子中你可以这样做:

FileWriter writer = new FileWriter(output_file);

try (Stream<String> lines = Files.lines(Paths.get(input_file))) {
    lines.forEach(new Consumer<String>() {
        int i = 0;
        void accept(String line) {
            try {
                writer.write((i++) + " # " + line + System.lineSeparator());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    writer.close();
}
Run Code Online (Sandbox Code Playgroud)


i_a*_*ero 6

Java doc中所述:

使用但未在lambda表达式中声明的任何局部变量,形式参数或异常参数必须声明为final或者是有效的final(§4.12.4),否则在尝试使用时会发生编译时错误.

这意味着您的变量必须是最终的或有效的最终变量.你想要添加一个计数器forEach,你可以使用AtomicIntegerOldCurumudgeon建议的,这是IMO的首选方法.

我相信您也可以使用只有一个值的数组0,您可以将其用作计数器.检查并告诉我以下示例是否适合您:

public void test() throws IOException {
    FileWriter writer = new FileWriter("OutFile.txt");
    final int[] count = {0};

    try (Stream<String> lines = Files.lines(Paths.get("InFile.txt"))) {
        lines.forEach(line -> {
            try {
                count[0]++;
                writer.write(count[0] + " # " + line + System.lineSeparator());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        );
        writer.close();
    }
}
Run Code Online (Sandbox Code Playgroud)