1

Why I am getting java.util.ConcurrentModificationException even when I am using removeIf?What change is required in my code ?

List<String> namesList = new ArrayList<>();
        namesList.add("AA");
        namesList.add("BB");
        namesList.add("CC");
List<String> createList =null;
                namesList.stream()
            .map(m->  { namesList.removeIf( m1 -> m1=="AA" ) ; return m;   }   )
           .collect(Collectors.toList())
           .forEach(System.out::println);
3
  • You can replace it with namesList.remove("AA") Commented Aug 8, 2019 at 10:28
  • Note that you're comparing strings with ==. Never do that. Use equals() instead. Commented Aug 8, 2019 at 10:43
  • Exception because you are deleting item while iterating them. ArrayList is thread unsafe. so Use Collections.synchronizedCollection(namesList); Commented Aug 8, 2019 at 11:30

3 Answers 3

3

This is happening as your are modifying the source of the stream which is a non-concurrent ArrayList. The ArrayListSpliterator does not have the CONCURRENT or IMMUTABLE characteristic and it does not support removal during iteration. It is not due to usage of the Collection.removeIf method.

From the docs:

A Spliterator that does not report IMMUTABLE or CONCURRENT is expected to have a documented policy (for example throwing ConcurrentModificationException concerning structural interference detected during traversal.

For this to work you need to have List implementation which supports concurrent removal during iteration like a CopyOnWriteArrayList, whose Spliterator has the IMMUTABLE characteristic.

Anyway, you are not using the map operation correctly if you want to conditionally filter items from the List you should use Stream.filter instead also consider using equals while comparing String objects:

namesList.stream()
         .filter(m1 -> !"AA".equals(m1))
         .forEach(System.out::println);
Sign up to request clarification or add additional context in comments.

1 Comment

@AndyTurner I missed to see the .collect there! it is totally redundant. Edited
1

Assuming you are trying to remove the value "AA" from your list, here are your options:

  • namseList.removeIf(s -> s.equals("AA")) - no need to deal with Streams.
  • namesList.stream().filter(s -> !s.equals("AA")).collect(Collectors.toList());

Some points:

  • You are calling .collect() on a Stream followed by forEach(). This can be simplified to simply call forEach() on the Stream object.
  • You are comparing Strings using ==. This can do what you intend (i.e. compare their values, because of String interning), but it is good practice to compare values using .equals().
    • the call: .map(m -> {namesList.removeIf(...); return m}) is equivalent to .map(m -> m) (i.e. it does not do anything).

You probably want to use something like: List namesList = ... namesList.stream().filter(s -> !s.equals("AA")).forEach(System.out::println);

Comments

0

ConcurrentModificationException

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it.

This exception occurs because you are modifying the stream source.

namesList.stream().map(m->  { namesList.removeIf( m1 -> m1=="AA" ) ; return m;   }   )

you can simply call below code:

namesList.removeIf( m1 -> m1=="AA" )

Or As @Andy Turner suggested:

nameList.removeAll(Collections.singleton("AA"));

6 Comments

"you can simply call below code:" nameList.remove("AA"); would be simpler again.
@AndyTurner it only removes the first occurrence of the specified element from this list. docs.oracle.com/javase/8/docs/api/java/util/…
Then nameList.removeAll(Collections.singleton("AA"));.
I think removeAll take collection rather then object to remove docs.oracle.com/javase/8/docs/api/java/util/…
It does. Collections.singleton is a Collection (a Set, more specifically).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.