LinkedTransferQueue forEach() method in Java with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The forEach() method of Java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to traverse each element in this queue. Syntax: public void forEach(Consumer<E> action) Parameters: This method takes a parameter action which represents the action to be performed for each element. Return Value: This method does not returns anything. Exceptions: This method throws NullPointerException if the specified action is null. Below program illustrates the forEach() function of LinkedTransferQueue class: Program 1: Java // Java code to illustrate // forEach() method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // Create object of LinkedTransferQueue LinkedTransferQueue<Integer> LTQ = new LinkedTransferQueue<Integer>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add(6); LTQ.add(3); LTQ.add(5); LTQ.add(15); LTQ.add(20); // Prints the Deque System.out.println("Linked Transfer Queue : " + LTQ); System.out.println("Traversing this Queue : "); // Traverse this queue using forEach() method LTQ.forEach((n) -> System.out.println(n)); } } Output: Linked Transfer Queue : [6, 3, 5, 15, 20] Traversing this Queue : 6 3 5 15 20 Program 2: Java // Java code to illustrate // forEach() method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // Create object of LinkedTransferQueue LinkedTransferQueue<String> LTQ = new LinkedTransferQueue<String>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add("GeeksforGeeks"); LTQ.add("Geeks"); LTQ.add("Computer Science"); LTQ.add("Portal"); LTQ.add("Gfg"); // Prints the Deque System.out.println("Linked Transfer Queue : " + LTQ); System.out.println("Traversing this Queue : "); // Traverse this queue using forEach() method LTQ.forEach((n) -> System.out.println(n)); } } Output: Linked Transfer Queue : [GeeksforGeeks, Geeks, Computer Science, Portal, Gfg] Traversing this Queue : GeeksforGeeks Geeks Computer Science Portal Gfg Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#forEach-java.util.function.Consumer- Comment More infoAdvertise with us Next Article LinkedTransferQueue removeIf() method in Java with Examples P ProgrammerAnvesh Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-LinkedTransferQueue +1 More Practice Tags : JavaJava-Collections Similar Reads LinkedBlockingDeque forEach() method in Java with Examples The forEach() method of LinkedBlockingDeque performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Syntax: public void forEach(Consumer<E> action) Parameters: This method takes a parameter action which represents the 2 min read LinkedTransferQueue toArray() method in Java with Examples public Object[] toArray() The toArray() method of Java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to form an array of the same elements as that of LinkedTransferQueue. Basically, it copies all the element to the new array from the LinkedTransferQueue. Syntax: p 3 min read LinkedTransferQueue toString() method in Java with Examples The toString() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to return the string representation of the collection. The String representation consists of the elements of the collection enclosed in "[]" and separated by ", ". The order of elements in 2 min read LinkedTransferQueue transfer() method in Java with Examples The transfer() method of class LinkedTransferQueue is an inbuilt function in Java which is generally used to transfer an element to a thread which is waiting to receive it, if there is no thread waiting then it will wait till a thread comes to waiting state as soon as waiting thread arrives element 2 min read LinkedTransferQueue removeIf() method in Java with Examples The removeIf() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove all of the elements of this queue that satisfies a given predicate filter which is passed as a parameter to the method. Syntax: public boolean removeIf(Predicate filter) Paramete 2 min read LinkedTransferQueue retainAll() method in Java with Examples The retainAll() method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to retain all the elements in this queue which are common to both i.e. the specified collection and this queue. All other elements which are not common are removed from this queue. Syntax 3 min read Like