64

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?

8
  • hi guys, I am new in stackoverflow and don't know much functionality about this site. I found this issue while working and I just thought that this will be helpful to others. so added this question. Sep 13, 2011 at 9:23
  • 1
    You should probably read the FAQ. This is not the place to post questions you know the answer of Sep 13, 2011 at 9:26
  • 2
    @deepankar You can answer your question. SO do not write your answer in same question add below and accept it. This question will not remain as unanswered question
    – Pradeep
    Sep 13, 2011 at 9:28
  • 3
    btw... thank you, this save me a lot of time ;-) Jan 24, 2013 at 16:09
  • 8
    I have solved this issue by myself. Initially I was blank about the issue. But after some research I found that Arrays.asList(s) returns unmodifiable collection and so it throws an UnsupportedOperationException. We can solve this problem by creating a new collection with that list Collection c= new ArrayList(Arrays.asList(s)); or Collection c= new HashSet(Arrays.asList(s)); Dec 2, 2014 at 5:48

6 Answers 6

57

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]

2
31

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)

1
  • 3
    @Michel most of the work is done by java.util.AbstractList, but all optional methods of that throw UnsupportedOperationException. Public implementations like ArrayList override these no-op methods, but the internal implementation Collections$Arraylist used by Arrays.asList(...) does not override the add method. (but it does override the set method mentioned above) Aug 13, 2012 at 13:05
8

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.

8

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");

1

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

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

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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.