简体   繁体   English

Java在没有ConcurrentModificationException的情况下在同一循环中添加/删除

[英]Java add/remove at the same loop without ConcurrentModificationException

I have a following Java code: 我有以下Java代码:

if (value instanceof Collection) {
    Collection collection = (Collection) value;
    Collection updatedObjects = new ArrayList<>();
    for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
        Object object = iterator.next();
        if (object instanceof String) {
            iterator.remove();
            updatedObjects.add(StringUtils.wrapInSingleQuotes((String) object));
        } else if (object instanceof Date) {
            iterator.remove();
            updatedObjects.add(((Date) object).getTime());
        }
    }
    collection.addAll(updatedObjects);
}

Is it possible to rewrite this code in a more efficient way in order to avoid new ArrayList allocation ? 是否有可能以更有效的方式重写此代码,以避免分配新的ArrayList If so, please show an example. 如果是这样,请举例说明。

Having Collection of different types is bad practice, anyway you can use Java 8 streams: 拥有不同类型的Collection是不好的做法,无论如何,您都可以使用Java 8流:

return collection.stream().map(object -> {
        if (object instanceof String) {
            return StringUtils.wrapInSingleQuotes((String) object);
        } else if (object instanceof Date) {
            return ((Date) object).getTime();
        }
    }).collect(Collectors.toList());

Also you can just avoid calling iterator.remove() and before the last line write 您也可以避免在最后一行写之前调用iterator.remove()

collection.clear();
collection.addAll...

If you want to update the value of the variable collection because is a parameter varialble, for example you can follow the logic in java.util.List.sort implementation. 例如,如果由于参数varialble想要更新变量collection的值,则可以遵循java.util.List.sort实现中的逻辑。

    Object[] updatedObjects = collection.toArray();
    //fill the array updatedObjects 
    ListIterator<E> i = collection.listIterator();//this works only if collection is a list
    for (Object e : updatedObjects ) {
        i.next();
        i.set((E) e);
    }

暂无
暂无

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

相关问题 如何在没有concurrentmodificationexception的for循环中向java中的arraylist添加元素 - How to add elements to an arraylist in java inside a for loop without concurrentmodificationexception Java ConcurrentModificationException; 单线程; 没有为每个循环 - Java ConcurrentModificationException; single Thread; without for each loop 从 ArrayList 同时删除两个对象而不导致 ConcurrentModificationException? - Remove two objects at the same time from an ArrayList without causing a ConcurrentModificationException? 带有嵌套循环的java.util.ConcurrentModificationException移除调用 - java.util.ConcurrentModificationException with nested loop remove call For循环中的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException in For loop Java中的ConcurrentModificationException即使没有迭代 - ConcurrentModificationException in java even without iteration 如何在java中将两个迭代器保留在map上,并在没有ConcurrentModificationException的情况下删除其间的键 - How to keep two iterators over map in java and remove keys in between without ConcurrentModificationException 如何在没有ConcurrentModificationException的情况下删除List迭代 - How to remove List iteration without ConcurrentModificationException 如何在没有ConcurrentModificationException的情况下删除哈希图中的多个映射? - How to remove multiple mappings in hashmap without ConcurrentModificationException? 如何使用线程将对象添加到列表并同时使用该列表而不会获得 ConcurrentModificationException? - How to add objects to a list with a thread and use the list at the same time without getting ConcurrentModificationException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM