简体   繁体   English

Java - 从对象中获取子对象的递归仅适用于额外列表

[英]Java - Recursion to get children from objects only works with extra list

I'm using a recursive method to get all children from an object.我正在使用递归方法从 object 中获取所有孩子。

Class for the object: Class 用于 object:

class Mandant {
    private String id;
    private String parentId;

    public Mandant(String id, String parentId) {
        this.id = id;
        this.parentId = parentId;
    }

plus getter and setter.加上 getter 和 setter。

The method:方法:

private static List<Mandant> getAllChildren(Mandant mandant, List<Mandant> allMandants) {
        List<Mandant> result = new ArrayList<>();
        for (Mandant man: allMandants)
            if (man.getParentId() != null && man.getParentId().equals(mandant.getId())) {
                result.add(man);
        }
        for (Mandant man: result) {
            result.addAll(getAllChildren(man, allMandants));
        }
        return result;
    }

This throws an exception:这会引发异常:

Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967) Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013) at java.base/java.util.ArrayList$Itr.next(ArrayList.java :967)

But if I'm using an extra list for storing the recursive call it works:但是,如果我使用一个额外的列表来存储递归调用,它就可以工作:

private static List<Mandant> getAllChildren(Mandant mandant, List<Mandant> allMandants) {
            List<Mandant> result = new ArrayList<>();
            for (Mandant man: allMandants)
                if (man.getParentId() != null && man.getParentId().equals(mandant.getId())) {
                    result.add(man);
            }
            List<Mandant> lowerChildren = new ArrayList<>();
            for (Mandant man: result) {
                lowerChildren.addAll(getAllChildren(man, allMandants));
            }
            result.addAll(lowerChildren);
            return result;
        }

Does anyone know why?有谁知道为什么?

The ConcurrentModificationException is an Exception, thrown when you try to modify a list while iterating through it. ConcurrentModificationException 是一个异常,当您在迭代列表时尝试修改列表时抛出。 You should not iterate over a list an remove objects during this process.在此过程中,您不应遍历列表并删除对象。

I will explain several ways to avoid this exception:我将解释避免这种异常的几种方法:

  1. Don't remove during iteration.不要在迭代期间删除。 That is exactly what you've done.这正是你所做的。

  2. Use a Iterator directly, this will prevent getting an Exception.直接使用迭代器,这将防止出现异常。

  3. Make use of "removeIf()".使用“removeIf()”。 You can remove objects from a list, using functional programming (Lambda) inside the method call.您可以使用方法调用中的函数式编程 (Lambda) 从列表中删除对象。

  4. You can filter the List, using Streams.您可以使用 Streams 过滤列表。 This is more advanced and actually not really necessary.这是更高级的,实际上并不是真正必要的。 But you will not face a ConcurrentModificationException doing this.但是你不会遇到 ConcurrentModificationException 这样做。

From all these ways, I use Iterators the most, but I think you should try and test.从所有这些方式中,我使用迭代器最多,但我认为你应该尝试和测试。 Every way has its own usecase.每种方式都有自己的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM