Problem One: Copyonwritearraylist cannot be cast to ArrayList
Workaround: Pass copyonwritearraylist into the ArrayList
ArrayList<T> arrayList = new ArrayList<T>(list);
Issue two: Copyonwritearraylist does not support Collections.sort
The workaround:
new ArrayList<T>(list); Collections.sort(arrayList ,new Comparator<T>() { publicintcompare(T o1, T o2) {} });
Question three: Copyonwritearraylist does not support
while (Iterator.hasnext ()) {
Iterator.remove ();
}
Reason: Copyonwritearraylist did a "snapshot" before doing the iteration, so the ITER is immutable at this point, that is to say that calling Iter.remove () in this traversal throws an exception
The workaround:
CopyOnWriteArrayList<T> t1 ; Iteratoriterator = t1.iterator(); while (iterator{ T t= iterator.next(); t1.remove(t); }
Copyonwritearraylist Operation Java.lang.UnsupportedOperationException