Different ways to iterate a list

Published May 18, 2022

The Collection framework provides a List interface that allows us to maintain an ordered collection of objects. The List interface is implemented by ArrayList, LinkedList, Stack, and Vector. Many Java programs use ArrayList and LinkedList. Throughout this article, we will learn how to iterate through a list in Java. 

How to Iterate a List in Java

You can use three methods to iterate a list in Java. These  four methods  include:

  •  Using the Enhanced For Loop Method

  • Using the iterator method

  • ForeEach iterable

 

a) Using the Enhanced For Loop Method

This is a more compact version of the basic for loop that can be used to iterate through a list:

 

example

import java.util.ArrayList;

import java.util.List;

public class Enhanced_for_loop {

            public static void main(String[] args) {

        List<Integer> figurez = new ArrayList<>();

        figurez.add(100);

        figurez.add(200);

        figurez.add(300);

        figurez.add(400);

        for(int i: figurez) {

            System.out.print(i+ " ");

        }

            }

}

 

Output

100 200 300 400

 

Using Iterator Method

You can also use iterators to iterate over a list. Iterators are especially useful when you need to modify the list that is being iterated.

Example

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class Iterate_List_using_Iterators {

            public static void main(String[] args) {

                        List<Integer> numbari = new ArrayList<>();

                        numbari.add(100);

                        numbari.add(200);

                        numbari.add(300);

                    Iterator<Integer> itr = numbari.iterator();

                    while(itr.hasNext()) {

                        int i = itr.next();

                        System.out.print(i+ " ");

                        if(i==3) {

                            itr.remove();

                        }

 

            }

            }

}

 

Output

100 200 300

 

ForEach iterable

As a consequence of the introduction of lambda functions in Java, the forEach function was added, which allows you to iterate over any collection.

The following code shows how the forEach method can be used to iterate over a list:

Example

import java.util.ArrayList;

import java.util.List;

public class Iterate_List_using_ForeEach_iterable {

            public static void main(String[] args) {

                        List<Integer> numbari = new ArrayList<>();

                        numbari.add(1);

                        numbari.add(2);

                        numbari.add(3);

                        numbari.add(4);

                        numbari.forEach(System.out::println);

            }

}

 

Output

1

2

3

4

 

Keywords: Iterate List , List Iterator

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

108 Views