I have a collection c1<MyClass> and an array a<MyClass>. I am trying to convert the array to a collection c2 and do c1.removeAll(c2), But this throws UnsupportedOperationException. I found that the asList() of Arrays class returns Arrays.ArrayList class and the this class inherits the removeAll() from AbstractList() whose implementation throws UnsupportedOperationException.
Myclass la[] = getMyClass();
Collection c = Arrays.asList(la);
c.removeAll(thisAllreadyExistingMyClass);
Is there any way to remove the elements? please help
Arrays.asListreturns aListwrapper around an array. This wrapper has a fixed size and is directly backed by the array, and as such calls tosetwill modify the array, and any other method that modifies the list will throw anUnsupportedOperationException.To fix this, you have to create a new modifiable list by copying the wrapper list’s contents. This is easy to do by using the
ArrayListconstructor that takes aCollection: