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

Posted

技术标签:

【中文标题】删除父对象时如何删除由stream.flatMap创建的列表中的子对象?【英文标题】:How to delete children objects in list made by stream.flatMap when parent gets deleted? 【发布时间】:2022-01-22 14:06:57 【问题描述】:

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

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创建的员工列表中的员工?

【问题讨论】:

Prog_G 我有一个我愿意删除的参考,所以我使用:departments.remove(department); 快速回答是在删除部门后使用stream.flatMap 再次创建一个新流,或者在删除部门之前迭代您的员工列表并删除它们(重复的可能问题?) . @sorifiend 这正是我想做的,我想知道是否会有更好的选择,但看起来不是这样 如果您的列表不大,那么这是您最好的选择。 您可以编写一个由您的部门列表支持的只读列表实现。但是,当您按 get(index) 访问 List 时,性能可能会很差。 【参考方案1】:

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

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

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

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

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

【讨论】:

【参考方案2】:

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

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]]

【讨论】:

以上是关于删除父对象时如何删除由stream.flatMap创建的列表中的子对象?的主要内容,如果未能解决你的问题,请参考以下文章

在父对象中删除时从子对象中删除实体子对象的选项

如何从关系中删除子对象或父对象?

克隆 JavaScript 对象时如何排除键列表? [复制]

创建一个子游戏对象的副本,并在删除父游戏对象时它仍然存在

qt全局对象删除

JPA / Hibernate - 删除子项删除父项(从同一个表)