I suggest that you put each stream that generate a list in method. like this :
public static List<String> getListFirstName(List<Employee> employees) {
return employees.stream()
.map(e -> e.getFirstName())
.collect(Collectors.toUnmodifiableList());
}
public static List<String> getListLasttName(List<Employee> employees) {
return employees.stream()
.map(e -> e.getLastName())
.collect(Collectors.toUnmodifiableList());
}
public static List<String> getListFullTime(List<Employee> employees) {
return employees.stream()
.filter(e -> e.getStatus().equals(Status.FULL_TIME))
.map(e -> e.getFirstName()+" "+e.getLastName())
.collect(Collectors.toUnmodifiableList());
}
public static List<String> getListPartTime(List<Employee> employees) {
return employees.stream()
.filter(e ->e.getStatus().equals(Status.PART_TIME))
.map(e -> e.getFirstName()+" "+e.getLastName())
.collect(Collectors.toUnmodifiableList());
}
Create a method that return a Map<String,List> that take a List of employees and a string criteria to generate a Map (the key is the criteria and the value is a list contains the wanted data) :
public static Map<String, List<String>> getByCriteria(String criteria , List<Employee> employees) {
Map<String, List<String>>map = new HashMap<>();
if("fName".equals(criteria)) {
map.put(criteria, getListFirstName(employees));
}else if("lName".equals(criteria)) {
map.put(criteria, getListFirstName(employees));
}else if("fullTime".equals(criteria)) {
map.put(criteria, getListFullTime(employees));
}else if("partTime".equals(criteria)) {
map.put(criteria, getListPartTime(employees));
}
return Collections.unmodifiableMap(map);
}
Create a List of criteria like :
List<String> criterias = List.of("fName", "lName", "fullTime", "partTime");
Create a stream from criterias list to generate a final Map that contain what you want
Map<String, List<String>> collect = criterias.stream().parallel().map(c -> getByCriteria(c , employees)).flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(eM -> eM.getKey(), eM -> eM.getValue()));
And here the complete class:
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
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));
List<String> criterias = List.of("fName", "lName", "fullTime", "partTime");
Map<String, List<String>> collect = criterias.stream().parallel().map(c -> getByCriteria(c , employees)).flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(eM -> eM.getKey(), eM -> eM.getValue()));
System.out.println(collect);
}
public static Map<String, List<String>> getByCriteria(String criteria , List<Employee> employees) {
Map<String, List<String>>map = new HashMap<>();
if("fName".equals(criteria)) {
map.put(criteria, getListFirstName(employees));
}else if("lName".equals(criteria)) {
map.put(criteria, getListFirstName(employees));
}else if("fullTime".equals(criteria)) {
map.put(criteria, getListFullTime(employees));
}else if("partTime".equals(criteria)) {
map.put(criteria, getListPartTime(employees));
}
return Collections.unmodifiableMap(map);
}
public static List<String> getListFirstName(List<Employee> employees) {
return employees.stream()
.map(e -> e.getFirstName())
.collect(Collectors.toUnmodifiableList());
}
public static List<String> getListLasttName(List<Employee> employees) {
return employees.stream()
.map(e -> e.getLastName())
.collect(Collectors.toUnmodifiableList());
}
public static List<String> getListFullTime(List<Employee> employees) {
return employees.stream()
.filter(e -> e.getStatus().equals(Status.FULL_TIME))
.map(e -> e.getFirstName()+" "+e.getLastName())
.collect(Collectors.toUnmodifiableList());
}
public static List<String> getListPartTime(List<Employee> employees) {
return employees.stream()
.filter(e ->e.getStatus().equals(Status.PART_TIME))
.map(e -> e.getFirstName()+" "+e.getLastName())
.collect(Collectors.toUnmodifiableList());
}
}
groupingBy,partitioningBy,mapping,collectingAndThen, etc. What I keep running into is I can modify and return single elements, but can't return a single element to multiple keys.forEachworks if I'm doing multiple passes, but can't figure out how to do it in one pass with immutability.