Copyonwritearraylist Operation Java.lang.UnsupportedOperationException

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.