Flatten the the object in java 8

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
    public static void main(String[] args) {
        String emp1Address[] = new String[]{"Gila Ave", "Knols Street", "Erlanger St."};
        String emp2Address[] = new String[]{"Convoy St", "Genesee Ave", "Engineer Rd."};
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee("Tom", Arrays.asList(emp1Address)));
        employees.add(new Employee("Yom", Arrays.asList(emp2Address)));
        Arrays.asList(flattenTheAddress(employees)).forEach(System.out:: println);
    }
    private static List<String> flattenTheAddress(List<Employee> employees){
        return employees.stream().map(emp -> emp.getAddress()).flatMap(address -> address.stream()).collect(Collectors.toList());
    }
private static List<Employee> excludeFromList(List<Employee> employees, List<Employee> excludedEmployee){
        Set<String> excludedSet = excludedEmployee.stream().map(emp -> emp.getName()).collect(Collectors.toSet());
        return employees.stream().filter(emp -> !excludedSet.contains(emp.getName())).collect(Collectors.toList());
    }

}

Comments