简体   繁体   English

如果某个元素存在于另一个元素中,则从Arraylist中删除它们,而不会引发ConcurrentModificationException

[英]Remove elements from an Arraylist if they are present in another one without raising ConcurrentModificationException

Here is the code: 这是代码:

Ledger obj = null;
MyUtilPojo obj1 = null;
Iterator it = toList.iterator();
while (it.hasNext()) {
    obj = (Ledger) it.next(); //after first iteration next here produce an error
    Iterator it1 = moreToList.iterator();
    while (it1.hasNext()) {
        obj1 = (MyUtilPojo) it1.next();
        if (obj.getId() == obj1.getKey()) {
            toList.remove(obj);                                
        }
    }
}

This raise an error ConcurrentModificationException , can someone help? 这会引发错误ConcurrentModificationException ,有人可以帮忙吗?

Something like that should work (constructs the new content for toList in a temp list): 这样的事情应该起作用(在临时列表中构造toList的新内容):

final List<Ledger> target = new ArrayList<Ledger>();
for (final Ledger led : toList) {
    for (final MyUtilPojo mup : moreToList) {
        if (led.getId() != mup.getKey()) { // beware of !=
            target.add(led);
        }
    }
}

toList = target;

ConcurrentModificationException occurs when you modify the list (by adding or removing elements) while traversing a list with Iterator. 当您使用Iterator遍历列表时(通过添加或删除元素)修改列表时,会发生ConcurrentModificationException。

    Ledger obj = null;
    MyUtilPojo obj1 = null;
    List thingsToBeDeleted = new ArrayList();

    Iterator it = toList.iterator();
    while (it.hasNext()) {
        obj = (Ledger) it.next();
        Iterator it1 = moreToList.iterator();
        while (it1.hasNext()) {
            obj1 = (MyUtilPojo) it1.next();
            if (obj.getId() == obj1.getKey()) {
                thingsToBeDeleted.add(obj);    // put things to be deleted                            
            }
        }
    }
    toList.removeAll(thingsToBeDeleted);

暂无
暂无

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

相关问题 从另一个arraylist中删除一个arraylist元素的最佳方法 - Best way to remove one arraylist elements from another arraylist 从 ArrayList 同时删除两个对象而不导致 ConcurrentModificationException? - Remove two objects at the same time from an ArrayList without causing a ConcurrentModificationException? 从ArrayList中删除随机项目,导致ConcurrentModificationException - Remove Random Item From ArrayList Causing ConcurrentModificationException 如何基于Java中对象的一个​​成员变量从数组列表中删除另一个数组列表中存在的对象? - How do I remove objects from an arraylist which are present in another arraylist based on one member variable of the object in Java? 从ArrayList中删除元素,同时保留重复项(如果存在) - Remove elements from ArrayList while retaining the duplicates if any present 如何在没有concurrentmodificationexception的for循环中向java中的arraylist添加元素 - How to add elements to an arraylist in java inside a for loop without concurrentmodificationexception 用另一个元素创建新的ArrayList - creating new ArrayList with elements from another one 将元素从一个ArrayList移动​​到另一个 - Moving elements from one ArrayList to another 如何仅将不同元素从一个arrayList复制到另一个ArrayList - How to copy only distinct elements from one arrayList to another ArrayList Java:将元素从一个ArrayList传输到另一个ArrayList - Java: transfering elements from one ArrayList to another ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM