There is a Student
class which has name, surname, age
fields and getters for them.
Given a stream of Student
objects.
How to invoke a collect
method such that it will return Map
where keys are age
of Student
and values are TreeSet
which contain surname
of students with such age
.
I wanted to use Collectors.toMap()
, but got stuck.
I thought I could do like this and pass the third parameter to toMap
method:
stream().collect(Collectors.toMap(Student::getAge, Student::getSurname, new TreeSet<String>()))`.