Monday, January 6, 2014

How to Remove Objects from Collection or ArrayList in Java while Traversing - Iterator remove() method Example

How do you remove objects from Java collections like ArrayList, while iterating is one of the frequent questions my reader asked to me in my post about Top 25 Java Collection Interview Questions. Well, this question may seems quite easy, because every java.util.Collection implementation e.g. List or Set has remove() method to delete a particular object, which can be used to remove elements from any Collection e.g. ArrayList, LinkedList or Vector. Well, this is where things goes wrong and interviewers are interested to see, whether you can point about remove() method from Iterator or not.

Answer of this question is as simple as that, you should be using Iterator's remove() method to delete any object from Collection you are iterating, but this is not the end of this question.

Most likely you will be asked to explain, what is difference in removing object using remove() method of Collection over remove() method of Iterator and why one should use over other?

Reason is ConcurrentModificationException, if you use remove() method of List, Set or basically from any Collection to delete object while iterating, it will throw ConcurrentModificationException. 

Though remove() method of java.util.Collection works fine to remove individual object, they don't work well, when you are iterating over collection.  Let's see a code example to clear doubts

How to Remove elements From ArrayList while Iterating

Java Tips to remove objects from ArrayList while iteratingIn below code, we have a list of exchanges and we are removing exchanges which are closed at the moment. Code is as simple as it could be and it's hard to find anything wrong on it by just looking, but things will be different when you run it. You will be hit by ConcurrentModificationException, as soon as you run, because here are we are using remove() method of ArrayList to remove objects, instead of Iterator's remove method. In order to fix ConcurrentModificationException, just use remove() method of java.util.Iteator class.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

  * Java program to demonstrate how to remove object form List and differnece
  * between Iterator's remove() method and Colection's remove() method in Java
public class ObjectRemovalTest {
    public static void main(String args[]) {
       List markets = new ArrayList();
       StockExchange TSE = new StockExchange(){
            public boolean isClosed() {
                return true;
       StockExchange HKSE = new StockExchange(){

            public boolean isClosed() {
                return true;
       StockExchange NYSE = new StockExchange(){

            public boolean isClosed() {
                return false;
       Iterator itr = markets.iterator();
           StockExchange exchange =;
               markets.remove(exchange); //Use itr.remove() method

interface StockExchange{
    public boolean isClosed();

Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(
        at java.util.AbstractList$
        at ObjectRemovalTest.main(
Java Result: 1

to be frank, even with the use of modern IDE like Eclipse, you may code it wrong, and end up confusing yourself when you see ConcurrentModificationException, because that sometime mislead programmers. It looks that may be another thread is modifying collection, and with this thought you won't look the code you are using for traversing ArrayList. That's why sometime interviewer present code and ask you to find bugs on it, or they may simply ask you to write code for removing objects from Collection while traversing over them. Always remember to use Iterator's remove() method for removing objects from Collection in Java.

Further Learning
Java Fundamentals: The Java Language by Jim Wilson
Java Fundamentals: Collections
Head First Java

Other Java Collection tutorials you may like
How to sort a Map by keys and values in Java? (tutorial)
How to sort an ArrayList in ascending and descending order in Java? (tutorial)
What is the difference between ArrayList and HashSet in Java? (answer)
What is the difference between TreeMap and TreeSet in Java? (answer)
What is the difference between HashMap and ConcurrentHashMap in Java? (answer)
The difference between HashSet and TreeSet in Java? (answer)
The difference between ArrayList and LinkedList in Java? (answer)
The difference between Vector and ArrayList in Java? (answer)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.


Haifeng Zhang said...

good article, thanks! One typo is has to be casting to StockExchange in the while loop.

java tutorials said...

Its better to use remove(index) if know the index of the object you want to remove, because then its not required to search that element in array. Only use remove(Object) if you don't know the index. See here to learn more about how to remove elements from an ArrayList.

Daniel Wong said...

I usually remove element by a reverse looping.
for(int i=list.size()-1;i≥0;i--)

Post a Comment