如何在删除父对象时删除 stream.flatMap 列表中的子对象?

How to delete children objects in list made by stream.flatMap when parent gets deleted?

假设我有一个部门 class,其中包含员工列表,如下所示:

public class Departement {
    List<Employees> employeesList;

    public List<Employees> getEmployeesList() {
        return employeesList;
    }

    public Departement setEmployeesList(List<Employees> employeesList) {
        this.employeesList = employeesList;
        return this;
    }
}

有一种情况,我需要在单独的列表中列出所有员工。为此,我使用流,如下所示:

List<Departement> departments ...
List<Employees> employees = departments.stream().flatMap(departement -> 
departement.getEmployeesList().stream()).collect(Collectors.toList()));

如果我在部门列表中删除一个部门,有没有办法自动删除stream.flatMap创建的员工列表中的员工?

没有直接的方法可以自动从员工列表中删除对象。您可以使用以下选项之一:

选项 1: 首先,删除待删除部门的所有员工,并将该部门从部门列表中删除。

employees.removeAll(departement2.getEmployeesList());
departments.remove(departement2);

选项 2: 从部门列表中删除部门,然后重新创建员工列表:

departments.remove(departement2);
employees = departments.stream().flatMap(departement ->departement.getEmployeesList().stream()).collect(Collectors.toList());

为了尝试,我实现了一个由父列表支持的通用列表类型:

public class FlatMapList<C,P> implements List<C>
{

    private Function<P,List<C>> getter;
    private List<P> parents;


    public FlatMapList(List<P> parents, Function<P,List<C>> getter)
    {
       this.parents = parents;
       this.getter = getter;

    }

    @Override
    public int size()
    {
        return getList().size();
    }

    @Override
    public boolean isEmpty()
    {
        return getList().isEmpty();
    }

    @Override
    public boolean contains(Object o)
    {
        return getList().contains(o);
    }

    @Override
    public Iterator<C> iterator()
    {
        return getList().iterator();
    }

    @Override
    public Object[] toArray()
    {
        return getList().toArray();
    }

    @Override
    public <T> T[] toArray(T[] a)
    {
        return getList().toArray(a);
    }

    @Override
    public boolean add(C e)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public boolean remove(Object o)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public boolean containsAll(Collection<?> c)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public boolean addAll(Collection<? extends C> c)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public boolean addAll(int index, Collection<? extends C> c)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public boolean removeAll(Collection<?> c)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public boolean retainAll(Collection<?> c)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public void clear()
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public C get(int index)
    {
        return getList().get(index);
    }

    @Override
    public C set(int index, C element)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public void add(int index, C element)
    {
        throw new UnsupportedOperationException("List is readonly");

    }

    @Override
    public C remove(int index)
    {
        throw new UnsupportedOperationException("List is readonly");
    }

    @Override
    public int indexOf(Object o)
    {
        return getList().indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o)
    {
        return getList().lastIndexOf(o);
    }

    @Override
    public ListIterator<C> listIterator()
    {
        return getList().listIterator();
    }

    @Override
    public ListIterator<C> listIterator(int index)
    {
        return getList().listIterator(index);
    }

    @Override
    public List<C> subList(int fromIndex, int toIndex)
    {
        return getList().subList(fromIndex,toIndex);
    }

    @Override
    public String toString( ) {
        return getList().toString();
    }

    private List<C> getList() {
        return Collections.unmodifiableList(parents.stream().flatMap(p ->
            this.getter.apply(p).stream()).collect(Collectors.toList()));
    }

    public static void main(String[] args) {

       Department dep1 = new Department();
       dep1.setName("D1");
       dep1.setEmployeesList(Arrays.asList(new Employee("e1"),new Employee("e2")));
       Department dep2 = new Department();
       dep2.setName("D2");
       dep2.setEmployeesList(Arrays.asList(new Employee("e3"),new Employee("e4")));
       List<Department> deps = new ArrayList<>(Arrays.asList(dep1,dep2));

       // Create a List backed with the parent list and the getter for the children
       List<Employee> employees = new FlatMapList<>(deps,Department::getEmployeesList);
       System.out.println(employees);
       deps.remove(dep1);
       System.out.println(employees);

    }
}

输出

[Employee [name=e1], Employee [name=e2], Employee [name=e3], Employee [name=e4]] 
[Employee [name=e3], Employee [name=e4]]