Youssef Idraiss
Youssef Idraiss

Reputation: 426

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

Let's say I have a department class that contains a list of employees, as follows:

public class Departement {
    List<Employees> employeesList;

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

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

There is a case where I need all employees on a separate list. To do that I'm using streams, as follows:

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

In case I delete a department in the department list, is there a way to automatically delete its employees in the employees list made by stream.flatMap?

Upvotes: 0

Views: 607

Answers (2)

Turo
Turo

Reputation: 4924

Just to try it I implemented a generic Type of a List backed by a parent List:

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);

    }
}

Output

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

Upvotes: 1

Prog_G
Prog_G

Reputation: 1615

There is no direct way to automatically delete the objects from the employee list. You can use one of the below options:

Option 1: First, delete all the employees of the to-be-deleted department and delete the department from the department list.

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

Option 2: Delete the department from the department list and then recreate the employee list:

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

Upvotes: 1

Related Questions