Let's say you have an interface, a couple of implementation classes and an Enum like this: Interface:
public interface Employee {
// No two employees will have same favorite hobbies
List<Hobby> favoriteHobbies;
}
Implementations:
public class Employee1 implements Employee {
@Override
public List<Hobby> favoriteHobbies {
return List.of(Hobbies.swimming, Hobbies.dancing);
}
}
public class Employee2 implements Employee {
@Override
public List<Hobby> favoriteHobbies {
return List.of(Hobbies.running);
}
}
Enum:
public enum Hobby {
swimming,
running,
dancing
}
I have
List<Employee> employees = List.of(Employee1, Employee2);
And using Streams, I want to Stream through employees
list and again Stream through each Employee object's favoriteHobbies()
and create a Map<Hobby, Employee>
like below:
"swimming" -> "Employee1"
"dancing" -> "Employee1"
"running" -> "Employee2"