I want to convert a List to a map of where the key is continent name and value is a list of students in each continent.
I have seen quite a few posts on stackoverflow but they were not giving me enough hints on how to go about it.
I have an internal map that specified the nationality <-> continent mapping
Hashmap<String,String> continentNationalityMap = new HashMap<>();
continentNationalityMap.put("USA", "North America");
continentNationalityMap.put("British", "Europe");
continentNationalityMap.put("Japan", "Asia");
continentNationalityMap.put("China", "Asia");
continentNationalityMap.put("Portugal", "Europe");
My student class is as below,
class Student{
String name;
String nationality;
int age;
}
What is the best way to convert this in java 8 using streams? Should I just do the lookup in map function and put it in a pre constructed map or is there a better way to do this with collectors?