I have a Student
object as below
class Student{
String name,email,country;
//getters setters
}
And I need collect the elements as TreeMap<String,List<String>>
where key is student's country
and value is the list of email
Map<String, List<Student>> countryStudents = students.stream()
.collect(Collectors.groupingBy(Student::getCountry));
Map<String,List<String>> map = new HashMap<>();
countryStudents .entrySet().forEach(entry -> map.put(entry.getKey(),entry.getValue().stream().map(student -> student .getEmail()).collect(Collectors.toList())));
Am wondering if is there any efficient way to do this instead of doing it in 2 iterations.