Wednesday, June 10, 2026
HomeLanguagesJavaLinkedTransferQueue forEach() method in Java with Examples

LinkedTransferQueue forEach() method in Java with Examples

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 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 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("Lazyroar");
        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 : [Lazyroar, Geeks, Computer Science, Portal, Gfg]
Traversing this Queue : 
Lazyroar
Geeks
Computer Science
Portal
Gfg

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#forEach-java.util.function.Consumer-

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS