2

I have a code that adds data to a list. What I do not understand is why the UnsupportedOperationException is thrown in one case and ConcurrentModificationException in the other. I am adding data in list in both the case and then trying to remove list data while iterating over the list. What i have read so far is that whenever any modification is made to fail- fast collection,ConcurrentModificationException is thrown. So why this different behavior in both these cases?

  List<String> animalList = new ArrayList<>();
        animalList.add("cat");
        animalList.add("dog");
        animalList.add("bear");
        animalList.add("lion");

        Iterator<String> iter = animalList.iterator();

        while(iter.hasNext()){
            String animal = iter.next();
            System.out.println(animal);
            animalList.remove(3);
        }

This code throws ConcurrentModificationException

String[] strings = { "Java", "Honk", "Test" };

        List<String> list = Arrays.asList(strings);


        Iterator<String> iterator = list.iterator();

        while(iterator.hasNext()){
            String name = iterator.next();
            System.out.println(name);
            list.remove(3);
        }

while this one throws UnsupportedOperationException

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
Asmat K
  • 121
  • 1
  • 10

2 Answers2

1

For the code block, where you get ConcurrentModificationException , you get that exception because you created an iterator on List then removing directly from list from within loop so iterator has issues. You should use remove() method of iterator itself - Iterator.remove().

You should directly remove an element from list when removing from outside iterator loop. See this another SO Question

In second case, with Arrays.asList , you get a List but actual list object might not be an ArrayList and remove(int index) operation is optional at List interface. See this

All in all, as far as UnsupportedOperationException is concerned, in first case you are guaranteed to working with an ArrayList and for that class, remove operation is supported , See this

For second case, you can't be so sure. Refer documentation of Arrays.asList where it says that returned list of fixed size so certain operations are not supported.

Community
  • 1
  • 1
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • Iterator.remove() didn't give the **ConcurrentModificationException**. So it means if i want to modify a collection while iterating over it, i should be using iterator's remove method not the class's remove method(in this case ArrayList) – Asmat K May 01 '17 at 06:13
0

Arrays.asList does not return an ArrayList. In fact, the list returned is not modifiable, thus when you try to modify it, it throws the UnsupportedOperationException.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • 1
    To be more accurate, the list returned by `Arrays.asList` is modifiable, but it has a fixed size, so `add` and `remove` are not supported. On the other hand `set` is supported. – Eran May 01 '17 at 05:21
  • As you are saying Arrays.asList is modifiable but only the size is fixed,so if i remove an element and also add another element during the same iteration, the size remains unchanged.Then also why am i getting the same exception? – Asmat K May 01 '17 at 05:26
  • you can not remove items from the list returned from Arrays.asList, period. It has nothing to do with iteration at all. – MeBigFatGuy May 01 '17 at 06:23