LinkedTransferQueue remove() method in Java

The java.util.concurrent.LinkedTransferQueue.remove() method is an in-built function in Java which is used to remove an element if it is present in this queue.

Syntax:

LinkedTransferQueue.remove(Object o)

Parameters: The function accepts a single parameter o i.e. the object to be removed.

Return Value: The function returns a true boolean value on successful removal of the object, otherwise returns false.

Below programs illustrate LinkedTransferQueue.remove() method:



Program 1: The element to be removed is present in the queue.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate remove()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueRemoveExample1 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
              new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 1; i <= 5; i++)
            queue.add(i);
  
        // Printing the elements of the queue
        System.out.println("The elements in the queue are:");
        for (Integer i : queue)
            System.out.print(i + " ");
  
        // remove() method will remove the specified
        // element from the queue
        queue.remove(1);
        queue.remove(5);
  
        // Printing the elements of the queue
        System.out.println("\nRemaining elements in queue : ");
        for (Integer i : queue)
            System.out.print(i + " ");
    }
}

chevron_right


Output:

The elements in the queue are:
1 2 3 4 5 
Remaining elements in queue : 
2 3 4

Program 2: The element to be removed is not present in the queue.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate remove()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueRemoveExample2 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
                    new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 10; i <= 15; i++)
            queue.add(i);
  
        // Printing the elements of the queue
        System.out.println("The elements in the queue are:");
        for (Integer i : queue)
            System.out.print(i + " ");
  
        // remove() method will remove the specified
        // element from the queue
        queue.remove(1);
        queue.remove(5);
  
        // Printing the elements of the queue
        System.out.println("\nRemaining elements in queue : ");
        for (Integer i : queue)
            System.out.print(i + " ");
    }
}

chevron_right


Output:

The elements in the queue are:
10 11 12 13 14 15 
Remaining elements in queue : 
10 11 12 13 14 15

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#remove(java.lang.Object)



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.