Skip to main content

Different ways to iterate ArrayList in Java

How to Iterate ArrayList in Java

In this article, we are going to learn about how to iterate ArrayList in Java. Java provides various ways to loop over the ArrayList that we are going to see one-by-one.

What is ArrayList in Java

ArrayList in Java is a List in which we can store and retrieve elements like an array but it provides a facility of Dynamic array or Resize-able Array i.e., An array which can grow and shrink its size at the time of addition or deletion of elements. To know more about the ArrayList, read this article: ArrayList in Java.

How to Iterate ArrayList in Java using Iterator?

ArrayList provides a method called iterator() which returns an object of Iterator. Iterator is an interface defined in java.util package and its implementation are provided by the ArrayList internally.
Look at the Syntax of how to get the object of Iterator from the ArrayList:
Syntax of iterator() method
Iterator<T> iterator = arrayList.iterator();
By using this iterator object, we can iterate ArrayList in Java. Iterator provides two methods to iterate ArrayList in java are:
  • hasNext() : returns true, if iterator has more elements for iteration.
  • next() : returns the next element from the Iterator.
With the combination of these two methods, we can iterate ArrayList in Java.

Java ArrayList Iterator Example 1: Iterate ArrayList using Iterators

ArrayList<String> bookList = new ArrayList<>();
bookList.add("Programming In Java");
bookList.add("Introduction to Algorithms");
bookList.add("Data Structures");
bookList.add("Learn C++");
bookList.add("Computer Networks");

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

while (iterator.hasNext()) {
      String nextElement = iterator.next();
      System.out.println(nextElement);
}
Output
Programming In Java
Introduction to Algorithms
Data Structures
Learn C++
Computer Networks

Exceptional Case

Any addition or deletion of elements onto the ArrayList during the Iteration is not allowed. In such cases, java.util.ConcurrentModificationException will be raised. Just look at the example:
Iterator<String> iterator = bookList.iterator();

while (iterator.hasNext()) {
      String nextElement = iterator.next();

      // ConcurrentModificationException will be raised 
      bookList.add("Add new Element to the List"); 

      System.out.println(nextElement);
}
Output
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909
        at java.util.ArrayList$Itr.next(ArrayList.java:859)
        at prac.code.ArrayListExample.main(ArrayListExample.java:56)

How to Iterate ArrayList in Java using for each loop?

ArrayList in Java implements Iterable Interface and any class whose implements Iterable interface can be iterate using for-each loop. Let's look at the example:

Java ArrayList Iterator Example 2: Iterate ArrayList using for-each loop

ArrayList<String> bookList = new ArrayList<>();
bookList.add("Programming In Java");
bookList.add("Introduction to Algorithms");
bookList.add("Data Structures");
bookList.add("Learn C++");
bookList.add("Computer Networks");

for(String book : bookList){
      System.out.println(book);
}

Output
Programming In Java
Introduction to Algorithms
Data Structures
Learn C++
Computer Networks
Note: Similar to Iterators, Any Addition or Modification of the elements into the ArrayList during Iteration is not allowed. In such cases, java.util.ConcurrentModificationException will be raised.

How to Iterate ArrayList in Java using forEach() method?

Since Java 8, a new method is added to the ArrayList which is forEach() method and this method can be used to iterate ArrayList in Java. The forEach() method takes an argument of the Consumer Functional Interface. This consumer object is passed as an argument of the forEach() method and consumes each element of the ArrayList one-by-one.
How to create object of Consumer
Consumer<String> consumer = (String book) -> {
      System.out.println(book); 
}
Note: Above, Lambda Expression is used to create object of Consumer. If you don't know about what is lambda expression, read this post: Lambda Expression in Java
Let's look at the example to better understand:

Java ArrayList Iterator Example 3: Iterate ArrayList using forEach() method

ArrayList<String> bookList = new ArrayList<>();
bookList.add("Programming In Java");
bookList.add("Introduction to Algorithms");
bookList.add("Data Structures");
bookList.add("Learn C++");
bookList.add("Computer Networks");

bookList.forEach((String book) -> {
      System.out.println(book);
});

Output
Programming In Java
Introduction to Algorithms
Data Structures
Learn C++
Computer Networks
Note: Similar to Iterators, Any Addition or Modification of the elements into the ArrayList during Iteration is not allowed. In such cases, java.util.ConcurrentModificationException will be raised.

Conslusion

In this article, we have learned different ways to iterate ArrayList in java. Java 8 onwards, forEach() method is the best and easiest way to iterate ArrayList. Also, we have learned what we cannot do during the Iteration of ArrayList.

Related Posts:


Comments