I have List of Customer object. I want to iterate over list and increment orders by one.
I tried with for each but here I had to create new list and add values in it.
class Customer{
long id;
int orders;
//getters setters constructor
}
List<Customer> empList=Arrays.asList(new Customer(1,10),new Customer(2,,20));
List<Customer> empList1=new ArrayList<>();
empList.forEach(e->{
e.orders++; //updating orders
empList1.add(e);
});
Is there a better way to do this? I tried with streams but it is only mapping the orders
empList.stream().map(e->{e.orders++;}).collect(Collectors.toList());
peek