Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


List<String> list = new LinkedList<String>(Arrays.asList(split));
template.split("\\|")

@Pentium: one more thing: you shouldn't be creating a new instance of Random everytime. Make it a static field and seed it only once.

Arrays.asList: Returns a fixed-size list backed by the specified array.

Create a LinkedList, which supports faster remove.

Instead of calling remove one at a time with random indices, it's better to generate enough random numbers in the range, and then traversing the List once with a listIterator(), calling remove() at appropriate indices. There are questions on stackoverflow on how to generate random but distinct numbers in a given range.

Is LinkedList really faster? Both LinkedList and ArrayList have O(n) remove here :\ It's almost always better to just use an ArrayList

LinkedList vs ArrayList -->There is a performance test graph from Ryan. LinkedList is faster in removing.

Quite a few problems with your code:

String.split(String regex): Splits this string around matches of the given regular expression.

Thanks, I have only limited elements in the string <10 so won't be an optimization problem.

With this, your algorithm would be O(N).

You can't add to it; you can't remove from it. You can't structurally modify the List.

| is a regex metacharacter; if you want to split on a literal |, you must escape it to \|, which as a Java string literal is "\\|".

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


List<String> list = new LinkedList<String>(Arrays.asList(split));
template.split("\\|")

@Pentium: one more thing: you shouldn't be creating a new instance of Random everytime. Make it a static field and seed it only once.

Arrays.asList: Returns a fixed-size list backed by the specified array.

Create a LinkedList, which supports faster remove.

Instead of calling remove one at a time with random indices, it's better to generate enough random numbers in the range, and then traversing the List once with a listIterator(), calling remove() at appropriate indices. There are questions on stackoverflow on how to generate random but distinct numbers in a given range.

Is LinkedList really faster? Both LinkedList and ArrayList have O(n) remove here :\ It's almost always better to just use an ArrayList

LinkedList vs ArrayList -->There is a performance test graph from Ryan. LinkedList is faster in removing.

Quite a few problems with your code:

String.split(String regex): Splits this string around matches of the given regular expression.

Thanks, I have only limited elements in the string <10 so won't be an optimization problem.

With this, your algorithm would be O(N).

You can't add to it; you can't remove from it. You can't structurally modify the List.

| is a regex metacharacter; if you want to split on a literal |, you must escape it to \|, which as a Java string literal is "\\|".

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


List<String> list = Arrays.asList(split);
List<String> list = new LinkedList<>(Arrays.asList(split));

@Dimitris: correct; it's modifiable, just not structurally.

@polygenelubricants: it seems you mix up unmodifiable and immutable. unmodifiable means exactly "modifiable, but not structurally".

Arrays.asList() is not an unmodifiable wrapper.

I just tried creating an unmodifiableList wrapper and trying a set; it throws UnsupportedOperationException. I'm quite certain Collections.unmodifiable* really means full immutability, not just structural.

Probably because you're working with unmodifiable wrapper.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


List<String> list = new ArrayList(Arrays.asList(split));

The list returned by Arrays.asList() might be immutable. Could you try

Wrong regarding the LinkedList. He is accessing by index, so LinkedList would spend as much time to find an element through iteration. See my answer for a better approach, using an ArrayList.

he's deleting, ArrayList is not the best data structure for deleting its values. LinkedList feets much more for his problem.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


List<String> list = new ArrayList(Arrays.asList(split));

The list returned by Arrays.asList() might be immutable. Could you try

Wrong regarding the LinkedList. He is accessing by index, so LinkedList would spend as much time to find an element through iteration. See my answer for a better approach, using an ArrayList.

he's deleting, ArrayList is not the best data structure for deleting its values. LinkedList feets much more for his problem.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


newList.addAll(Arrays.asList(newArray));

Create a new list with the same content:

Minor point, but you aren't "wrapping" the original list, you are creating a completely new list (which is why it works).

This one has burned me many times. Arrays.asList creates an unmodifiable list. From the Javadoc: Returns a fixed-size list backed by the specified array.

This will create a little extra garbage, but you will be able to mutate it.

Yup, I used Arrays.asList() in my JUnit test case, which then was stored inside my map. Changed my code to copy the list passed in, into my own ArrayList.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


This UnsupportedOperationException comes when you try to perform some operation on collection where its not allowed and in your case, When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


newList.addAll(Arrays.asList(newArray));

Create a new list with the same content:

Minor point, but you aren't "wrapping" the original list, you are creating a completely new list (which is why it works).

This one has burned me many times. Arrays.asList creates an unmodifiable list. From the Javadoc: Returns a fixed-size list backed by the specified array.

This will create a little extra garbage, but you will be able to mutate it.

Yup, I used Arrays.asList() in my JUnit test case, which then was stored inside my map. Changed my code to copy the list passed in, into my own ArrayList.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


String idList = "123,222,333,444";

List<String> parentRecepeIdList = new ArrayList<String>();

parentRecepeIdList.addAll(Arrays.asList(idList.split(","))); 

parentRecepeIdList.add("555");

Other than using a linked list, simply use addAll method list.

Yes, on Arrays.asList, returning a fixed-size list.

Note
Rectangle 27 1

java Why do I get an UnsupportedOperationException when trying to remove an element from a List?


This UnsupportedOperationException comes when you try to perform some operation on collection where its not allowed and in your case, When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.

Note