Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaLinkedTransferQueue retainAll() method in Java with Examples

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:

public boolean retainAll(Collection C)

Parameters: This method takes the parameter C which is the collection containing elements to be retained in the queue.

Returns: The method returns a boolean value true if the queue is changed at all as a result of the call else false.

Exceptions: This method throws following exceptions:

  • ClassCastException: If the class of an element of this Queue is incompatible with the Passed collection.
  • NullPointerException: If the specified collection is Null.

Below program illustrates the retainAll() function of LinkedTransferQueue class :

Program 1:




// Java code to illustrate
// retainAll() 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(3);
        LTQ.add(6);
        LTQ.add(10);
        LTQ.add(125);
        LTQ.add(205);
  
        // Print the Queue
        System.out.println("Linked Transfer Queue : "
                           + LTQ);
  
        // Get the ArrayList to be retained
        ArrayList<Integer> arraylist
            = new ArrayList<Integer>();
        arraylist.add(10);
        arraylist.add(100);
        arraylist.add(125);
  
        // Print ArrayList
        System.out.println("ArrayList to be retained : "
                           + arraylist);
  
        // Retaining elements from the queue
        // which are common to arraylist
        // using retainAll() method.
        LTQ.retainAll(arraylist);
  
        // Prints the Queue
        System.out.println("Linked Transfer Queue "
                           + "after retaining ArrayList : "
                           + LTQ);
    }
}


Output:

Linked Transfer Queue : [3, 6, 10, 125, 205]
ArrayList to be retained : [10, 100, 125]
Linked Transfer Queue after retaining ArrayList : [10, 125]

Program 2:




// Java code to illustrate
// retainAll() 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("Geek");
        LTQ.add("Gfg");
        LTQ.add("Computer");
        LTQ.add("Science");
  
        // Print the Queue
        System.out.println("Linked Transfer Queue : "
                           + LTQ);
  
        // Get the ArrayList to be retained
        ArrayList<String> arraylist
            = new ArrayList<String>();
        arraylist.add("Gfg");
        arraylist.add("Science");
        arraylist.add("Computer");
  
        // Print ArrayList
        System.out.println("ArrayList to be retained : "
                           + arraylist);
  
        // Retaining elements from the queue
        // which are common to arraylist
        // using retainAll() method.
        LTQ.retainAll(arraylist);
  
        // Prints the Queue
        System.out.println("Linked Transfer Queue "
                           + "after retaining ArrayList : "
                           + LTQ);
    }
}


Output:

Linked Transfer Queue : [Lazyroar, Geek, Gfg, Computer, Science]
ArrayList to be retained : [Gfg, Science, Computer]
Linked Transfer Queue after retaining ArrayList : [Gfg, Computer, Science]

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#retainAll-java.util.Collection-

RELATED ARTICLES

Most Popular

Recent Comments