Thursday, 6 August 2015

How to loop/iterate an ArrayList in Java

There are many ways to loop or iterate an ArrayList in Java. We can use the simple for loop, for-each loop (advanced for loop) available from Java 5 onwards, iterator or ListIterator (though not a preferred way if we are just sequentially looping through the elements of a list) and from Java 8 using Java 8 forEach statement that works with stream.

So the options are -
  • for loop
  • for-each loop
  • iterator
  • ListIterator
  • Java 8 forEach loop

Example code

Let's see a program that will use all these ways to loop/iterate an array list.

public class LoopListDemo {
    public static void main(String[] args) {
        // Using Diamond operator, so with ArrayList 
        // don't need to provide String, this option is available from 
        // Java 7 onwards
        List<String> cityList = new ArrayList<>();
        // Using for loop with size
        System.out.println("With simple for loop");
        for(int i = 0; i < cityList.size(); i++){
            System.out.println("City Name - " + cityList.get(i));
        // Using for-each loop 
        System.out.println("With for-each loop - Java 5");
        for(String name : cityList){
            System.out.println("City Name - " + name);
        // Using Iterator
        System.out.println("With iterator - Java 5");
        Iterator<String> itr = cityList.iterator();
            System.out.println("City Name - " +;
        // Using List Iterator - though not needed if doing sequential looping
        System.out.println("With List iterator - Java 5");
        ListIterator<String> ltr = cityList.listIterator();
            System.out.println("City Name - " +;
        //Using Java 8 iterable.forEach loop
        System.out.println("With for-each loop - Java 8");
        cityList.forEach((a)->System.out.println("City Name - " + a));
        //Using Java 8 iterable.forEach loop with method reference



Output (curtailed)

With simple for loop
City Name - Delhi
City Name - Mumbai
City Name - Bangalore
City Name - Chennai
City Name - Kolkata
With for-each loop - Java 5
City Name - Delhi
City Name - Mumbai
City Name - Bangalore
City Name - Chennai
City Name - Kolkata
With iterator - Java 5
City Name - Delhi
City Name - Mumbai
City Name - Bangalore
City Name - Chennai
City Name - Kolkata

In my opinion for-each loop is best way to iterate a list if you just need to traverse sequentially through a list.
With Java 8 and multi-core environment you can also use Java 8 forEach loop to take advantage of parallel computing using parallel stream. In that case you need to write it like -

cityList.parallelStream().forEach((a)->System.out.println("City Name - " + a));

That's all for this topic how to loop an ArrayList in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. How ArrayList works internally in Java
  2. How HashMap internally works in Java
  3. How LinkedList class works internally in Java
  4. How HashSet works internally in Java
  5. How to remove duplicate elements from an ArrayList in Java
  6. How to convert Array to ArrayList in Java
  7. Java Collections interview questions

You may also like -

No comments:

Post a Comment