I've a list of items. I want to process a set of items which are in the middle of the list. Ex: Assume a list of employees who have id, first name, last name and middle name as attributes. I want to consider all rows between lastName "xxx" and "yyy" and process them further. How can this be optimized in Java8? Optimization is my first concern. Tried using Java8 streams and parallel streams. But termination(break) is not allowed in foreach loop in Java8 streams. Also we cannot use the outside("start" variable below) variables inside foreach. Below is the code which I need to optimize:
boolean start = false;
for(Employee employee: employees) {
if(employee.getLastname().equals("yyy")) {
break;
}
if(start) {
// My code to process
}
if(employee.getLastname().equals("xxx")) {
start = true;
}
}
What is the best way to handle the above problem in Java8?