'How to count the iteration of lambda in Java streams
I have below solution with counter variable, is there any preferred way to get the counter of lambda iteration ?
class Employee {
String name;
int salary;
}
public class Calculator {
private static Integer counter;
public void method1() {
counter = 0; // Instead of this, is there any default counter ?
List<Employee> emp = getData()
List<Employee> updated = emp.stream().map(Calculator::increaseSalary).collect(Collectors.toList());
System.out.println("counter = " + counter);
}
private Employee increaseSalary(Employee emp) {
counter++;
emp.setSalary(50000);
return emp;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
