How do I add up and remove repeated objects from ArrayList?
userDetails.stream()
.collect(Collectors.toMap(
UserDetail::getName,
Function.identity(),
(left, right) -> {
left.setSalary(left.getSalary() + right.getSalary());
return left;
}
))
.values();
This will give you a Collection<UserDetail>
. You can copy that into an ArrayList
if needed, obviously.