java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source)

JavaArraysExceptionCollections

Java Problem Overview


I have tried below code

String s[]={"1","2","3","4"};  
Collection c=Arrays.asList(s);  
System.out.println(c.remove("1") +"  remove flag");  

System.out.println(" collcetion "+c);  

I was getting

Exception in thread "main" java.lang.UnsupportedOperationException  
at java.util.AbstractList.remove(Unknown Source)  
at java.util.AbstractList$Itr.remove(Unknown Source)  
at java.util.AbstractCollection.remove(Unknown Source)  
at test.main(test.java:26)  

Can anyone help me to solve this issue?

Java Solutions


Solution 1 - Java

Easy work around is just to pass in the List into an ArrayList's constructor.

For example:


String valuesInArray[]={"1","2","3","4"};

List modifiableList = new ArrayList(Arrays.asList(valuesInArray));
System.out.println(modifiableList.remove("1") + "  remove flag");

System.out.println(" collcetion "+ modifiableList);

Response:

> true remove flag > >collcetion [2, 3, 4]

Solution 2 - Java

Slight correction: no, it's not an unmodifiable Collection. It just doesn't support adding and removing elements, because it is backed by the supplied array and arrays aren't resizeable. But it supports operations like list.set(index, element)

Solution 3 - Java

I was having this problem, because I was also initializing my list with Arrays.asList:

List<String> names = Arrays.asList("a", "b", "c");

To solve the problem, I used addAll instead:

List<String> names = new ArrayList<String>();
names.addAll(Arrays.asList("a", "b", "c"));

This way you can edit the list, add new items or remove.

Solution 4 - Java

The List returned by Arrays.asList method of java.util.Arrays class is a fixed-size list object which means that elements cannot be added to or removed from the list.

So functions like Adding or Removing cannot be operated on such kind of Lists.

The solution to adding or removing without getting java.lang.UnsupportedOperationException is ->

List<String> strList= new ArrayList<>(Arrays.asList(strs));

//Then Add or Remove can be called on such List

newList.add("100");
newList.remove("100");

Solution 5 - Java

A one liner fix is to declare your list like this:

List<Integer> list2 = new ArrayList<>(Arrays.asList(0,8,1,5,7,0));

Solution 6 - Java

The following method

private void printCollection() {
  List<String> strings = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));

  System.out.println(strings.remove("1") + " remove flag");
  System.out.println("collection " + strings);
}

Will print

true remove flag
collection [2, 3, 4]

Note that true is the (successful) return value from remove().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepankar SarkarView Question on Stackoverflow
Solution 1 - JavaBojan PetkovicView Answer on Stackoverflow
Solution 2 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 3 - JavaAntónio AlmeidaView Answer on Stackoverflow
Solution 4 - JavarohitmView Answer on Stackoverflow
Solution 5 - JavaNicolasomeView Answer on Stackoverflow
Solution 6 - JavaBlair NangleView Answer on Stackoverflow