Thursday, December 11, 2025
HomeLanguagesJavaArrayDeque forEach() method in Java

ArrayDeque forEach() method in Java

The forEach() method of ArrayDeque is inherited from interface java.lang.Iterable. The operation is performed in the order of iteration if that order is specified by the method. Method traverses each element of the Iterable of ArrayDeque until all elements have been processed by the method or an exception occurs. Exceptions thrown by the Operation are passed to the caller.

Syntax:

public void forEach(Consumer<? super E> action)

Parameter: This method takes a parameter name action which represents the action to be performed for each element.

Returns: This method returns Nothing.

Exception: This method throw NullPointerException if the specified action is null.

Below programs illustrate forEach() method of ArrayDeque:

Example 1: To demonstrate forEach() method on ArrayDeque which contains a queue of String values.




// Java Program Demonstrate forEach()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // which contains string values
        ArrayDeque<String> cities = new ArrayDeque<String>();
  
        // Add Strings to list
        cities.add("Kolkata");
        cities.add("Delhi");
        cities.add("Bombay");
        cities.add("Pune");
  
        // forEach method of ArrayDeque and
        // print city names
        cities.forEach((n) -> System.out.println(n));
    }
}


Output:

Kolkata
Delhi
Bombay
Pune

Example 2: To demonstrate forEach() method on ArrayDeque which contains queue of Objects.




// Java Program Demonstrate forEach()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Objects
        ArrayDeque<batch> list = new ArrayDeque<batch>();
  
        // Add Objects to list
        list.add(new batch("CSE", 67));
        list.add(new batch("ECE", 57));
        list.add(new batch("IT", 90));
        list.add(new batch("ME", 78));
  
        // print result
        System.out.println("list of Objects:");
  
        // forEach method of ArrayDeque and
        // print student names
        list.forEach((n) -> print(n));
    }
  
    // printing details of object
    public static void print(batch n)
    {
        System.out.println("*******************************");
        System.out.println("Batch Name is " + n.name);
        System.out.println("No of Students are " + n.noOfStudents);
    }
}
  
// create a class
class batch {
  
    String name;
    int noOfStudents;
  
    batch(String name, int noOfStudents)
    {
        this.name = name;
        this.noOfStudents = noOfStudents;
    }
}


Output:

list of Objects:
*******************************
Batch Name is CSE
No of Students are 67
*******************************
Batch Name is ECE
No of Students are 57
*******************************
Batch Name is IT
No of Students are 90
*******************************
Batch Name is ME
No of Students are 78

Example 3: To demonstrate NullPointerException of forEach() method on ArrayDeque.




// Java Program Demonstrate forEach()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // which contains string values
        ArrayDeque<String> cities = new ArrayDeque<String>();
  
        // Add Strings to list
        cities.add("Kolkata");
        cities.add("Delhi");
        cities.add("Bombay");
        cities.add("Pune");
  
        try {
            // forEach method of ArrayDeque and
            // print city names
            cities.forEach(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

Reference:
https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#forEach(java.util.function.Consumer)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32444 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12026 POSTS0 COMMENTS
Shaida Kate Naidoo
6945 POSTS0 COMMENTS
Ted Musemwa
7197 POSTS0 COMMENTS
Thapelo Manthata
6890 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS