Different ways to Iterate in List in Java

Introduction

In this blog we will learn how to iterate through list in different ways in Java.

Using loop

This is most classic way of iterating through list and is widely used.

Basic for loop consists of initialization, terminating condition and next state update.

Below example showcases how to iterate thorough list using for loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        nums.add(60);

        for(int i=0;i<nums.size();i++){
            System.out.println(nums.get(i));
        }

    }
}
1
2
3
4
5
6
10
20
30
40
50
60

Using enchanced for loop

We can iterate thorough list using enchanced for loop as shown below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        nums.add(60);

        for(int num: nums){
            System.out.println(num);
        }

    }
}
1
2
3
4
5
6
10
20
30
40
50
60

Using iterators

Iterator

You can use iterator over the list elements using iterator() method.

iterator.hasNext() method on Iterator checks if element is present in the list.

iterator.next() method on Iterator returns the element in the list.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        nums.add(60);

        Iterator<Integer> iterator = nums.iterator();

        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}
1
2
3
4
5
6
10
20
30
40
50
60

ListIterator

ListIterator is a iterator which can traverse in both directions

iterator.hasPrevious() method on ListIterator checks if element is present in the list in previous direction

iterator.previous() method on ListIterator returns the element in the list in previous direction

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        nums.add(60);

        ListIterator<Integer> iterator = nums.listIterator();

        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println("----------Traversing back-------------");

        while(iterator.hasPrevious()){
            System.out.println(iterator.previous());
        }

    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
10
20
30
40
50
60
----------Traversing back-------------
60
50
40
30
20
10

Java 8 : forEach

We can use forEach method on the collection which takes lambda expression as the argument as shown below.

Similarly we can convert our collection to stream and using forEach to list all elements present in the list.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        nums.add(60);

        System.out.println("------------Iterable foreach--------------");
        nums.forEach(System.out::println);
        System.out.println("------------Stream foreach-----------------");
        nums.stream().forEach(System.out::println);

    }
}