
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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 ]