I'm attempting to take a list and return a Map<String, List<String> containing certain aspects of the original list, where the original elements could occur multiple times as values under different keys using streams. For example:
class Employee {
String firstName;
String lastName;
Status status;
}
enum Status {
FULL_TIME,
PART_TIME
}
List<Employee> employees = List.of(
new Employee("f1", "l1", Status.PART_TIME),
new Employee("f2", "l2", Status.FULL_TIME),
new Employee("f3", "l3", Status.PART_TIME),
new Employee("f4", "l4", Status.FULL_TIME));
The resulting map should have keys like the following and lists need to be immutable:
fName: ["f1", "f2", "f3", "f4"],
lName: ["l1", "l2", "l3", "l4"],
fullTime: ["f2 l2", "f4 l4"]
This would be easy with multiple streams, but the original data is quite large, so am trying to do it in one pass. I also have Guava and Apache Commons available if there is another method I'm not aware of. Thanks!
Edit 1: show how I'm doing it now
Here is what I'm doing in three streams that I'd like to simplify to one:
var firstName = employees.stream()
.map(e -> e.getFirstName())
.collect(Collectors.toUnmodifiableList());
var lastName = employees.stream()
.map(e -> e.getLastName())
.collect(Collectors.toUnmodifiableList());
var fullTime = employees.stream()
.filter(e -> e.getStatus().equals(Status.FULL_TIME))
.collect(Collectors.toUnmodifiableList());