I am trying to parse a MultipartFile line by line, and also count the total number of lines as it goes. Here's the snippet:
var inputStream = file.getInputStream();
var stream = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8)
).lines();
var totalTargetCount = 0l;
stream.forEach(line -> {
processTheLine(line);
totalTargetCount++;
});
But this gives the error Variable used in lambda expression should be final or effectively final What should I do in this case?
Found this that shows how to use a counter in stream's foreach, and I quote:
Method 1: Using Array
Method 2: Using AtomicInteger
Method 3: Using AtomicReference