Java - Iterate Through a List

By Preeti Jain Last updated : April 8, 2024

There are several approaches to iterate through a list in Java. Here, we will use the following three approaches:

  1. Using Regular Approach (Loops)
  2. Using Iterator
  3. Using Java Streams

Iterate Through a List using Loops

1. Using for loop

In this approach, we can traverse each element of the List by iteration using for, while, and for-each loops and each index of the List is identified by an index variable i.

Syntax

for(Initialization; Condition; Increment/Decrement){
  // functionality block to be executed
}

Example

// Program to iterate List elements 
// in Java by using for loop

import java.util.*;

public class IterateListElements {
  public static void main(String args[]) {
    // Create an empty Linked List
    List < Integer > list = new LinkedList < Integer > ();

    // Using add() method is to add elements
    // in the List
    list.add(10);
    list.add(30);
    list.add(50);
    list.add(70);
    list.add(90);
    
    // Using For loop is to traverse each element
    // of the List by an index variable (i)
    for (int i = 0; i < list.size(); i++) {
      // Using get(i) is to return an element at i index and
      // store it in a varaible named element
      Integer element = list.get(i);
      
      // Display List elements using
      // print statement
      System.out.println(element);
    }
  }
}

The output of the above example is:

10
30
50
70
90

2. Using while Loop

Similarly, you can traverse each element of the List by iteration using while Loop.

Syntax

while(Condition){
  // functionality block to be executed
}

Example

// Program to iterate List elements in Java 
// by using While loop
import java.util.*;

public class IterateListElements {
  public static void main(String args[]) {
    // Initialize variable i to 0 for While loop
    int i = 0;
    // Create an empty Linked List
    List < Integer > list = new LinkedList < Integer > ();

    // Using add() method is to add elements
    // in the List
    list.add(10);
    list.add(30);
    list.add(50);
    list.add(70);
    list.add(90);

    // Returns List length using list.size()
    int size = list.size();
    // If i<size returns true then inner block
    // will be executed

    while (i < size) {
      // Using get(i) is to return element at i index and
      // store it in a variable named element
      Integer element = list.get(i);

      // Display List elements using
      // print statement
      System.out.println(element);
      // Increment variable i by 1
      i++;
    }
  }
}

3. Using for-each Loop (Enhanced for Loop)

You can also traverse each element of the List by iteration using for-each loop and this is an optimized version of for loop.

Syntax

for(DataType variable_name: List_name){
  // functionality block to be executed
}

Example

// Program to iterate List elements in Java 
// by using For-each loop

import java.util.*;

public class IterateListElements {
  public static void main(String args[]) {
    // Create an empty Linked List
    List < Integer > list = new LinkedList < Integer > ();

    // Using add() method is to add elements
    // in the List
    list.add(10);
    list.add(30);
    list.add(50);
    list.add(70);
    list.add(90);

    // Iterate list elements using for-each Loop
    for (Integer i: list) {
      // Display List elements using
      // print statement
      System.out.println(i);
    }
  }
}

Iterate Through a List using Iterator

In this approach, the objective of the Iterator is to iterate an element of any collection object like List, Arrays, Vector, etc. You can access each element of the List using iterator.

Syntax

Iterator<DataType> identifier_name = List_name.iterator();

Example

// Program to iterate List element in Java 
// by using Iterator approach

import java.util.*;

public class IterateListElements {
  public static void main(String args[]) {
    // Create an empty Linked List
    List < Integer > list = new LinkedList < Integer > ();

    // Using add() method is to add elements
    // in the List
    list.add(10);
    list.add(30);
    list.add(50);
    list.add(70);
    list.add(90);

    // Create Iterator Object
    Iterator < Integer > itr = list.iterator();

    // itr.hasNext() checks for an element in List
    // and hasNext() returns true if an element exists
    // in the List
    while (itr.hasNext()) {
      // Display List elements using
      // print statement
      System.out.println(itr.next());
    }
  }
}

The output of the above example is:

10
30
50
70
90

Iterate Through a List using Java Streams

In this approach, we iterate list elements using forEach with stream() and this approach is fast and simple comparatively and it is introduced in Java 8.

Syntax

list_name.stream().forEach(any_variable_name->{
  //functionality to be executed
})

Example

// Program to iterate List element in Java
// Using Java Streams Approach [forEach() of Stream ]

import java.util.*;

public class IterateListElements {
  public static void main(String args[]) {
    // Create an empty LinkedList
    List < Integer > list = new LinkedList < Integer > ();

    // Using add() method is to add elements
    // in the List
    list.add(10);
    list.add(30);
    list.add(50);
    list.add(70);
    list.add(90);

    // list.stream().forEach(). First it converts list into stream
    // and then after displaying List elements using forEach()
    list.stream().forEach(
      (elements) -> System.out.println(elements));
  }
}

The output of the above example is:

10
30
50
70
90

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.