For example, I have some Student:
class Student{
private String name;
private int age;
}
And I have a list of Students:
Student st1 = new Student("Tom", 14);
Student st2 = new Student("Tom", 18);
Student st3 = new Student("Jack", 19);
So expected output should be like this:
finalList = [Student{name='Tom', age=32}, Student{name='Jack', age=19}]
============Worked solution=============
List<Student> result = rawListStudents.stream()
.collect(Collectors.toMap(Student::getName, Function.identity(), (v1, v2) -> new Student(v2.getName(), v1.getAge() + v2.getAge())))
.values()
.stream()
.toList();
Anyway, it seems there is no opportunity to merge list without additional steps with temporary map.