Java ArrayDeque forEach() Method



Description

The Java ArrayDeque forEach(E action) is used to performs a given action for each element of the Iterable until all elements are processed exception occurs during action. In case, an order is specified then actions are performed in the given order. In case of exception, the exception is relayed to the caller.

Declaration

Following is the declaration for java.util.ArrayDeque.forEach() method

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

Parameters

action − The action to be performed for each element.

Exception

NullPointerException − if the specified action is null.

Iterating ArrayDeque of Integers using forEach method Example

The following example shows the usage of Java ArrayDeque forEach(action) method to iterate and print Integers. We're adding couple of Integers to the ArrayDeque object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      Deque<Integer> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(1);
      deque.add(2);
      deque.add(3);
      deque.add(4);
      deque.add(5);

      // let us print all the elements available in deque
      deque.forEach(s -> { System.out.println(s);});
   }
}

Output

Let us compile and run the above program, this will produce the following result −

1
2
3
4
5

Iterating ArrayDeque of Strings using forEach method Example

The following example shows the usage of Java ArrayDeque forEach(action) method to iterate and print Strings. We're adding couple of Strings to the ArrayDeque object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("A");
      deque.add("B");
      deque.add("C");
      deque.add("D");
      deque.add("E");

      // let us print all the elements available in deque
      deque.forEach(s -> { System.out.println(s);});
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
B
C
D
E

Iterating ArrayDeque of Objects using forEach method Example

The following example shows the usage of Java ArrayDeque forEach(action) method to iterate and print Student objects. We're adding couple of Student objects to the ArrayDeque object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));

      // let us print all the elements available in deque
      deque.forEach(s -> { System.out.println(s);});
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_arraydeque.htm
Advertisements