Open In App

LinkedTransferQueue drainTo() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

drainTo(Collection c)

The drainTo(Collection c) method of java.util.concurrent.LinkedTransferQueue Class is an in-built function in Java which removes all the elements present in this queue and adds them to the provided collection. This is a more efficient way than repeatedly polling this queue.

There is also possibilities of failure encountered while attempting to add elements to collection c from the queue and due to that failure, elements is distributed between both collections when the associated exception is thrown. If a queue is tried to drainTo() to queue itself, then IllegalArgumentException will be thrown. If the specified collection is modified while the operation is in progress, the behavior of this operation is undefined. So for using such methods, one needs to take care of this type of situation to overcome exceptions.

Syntax:

public int drainTo(Collection c)

Parameters: The function accepts a mandatory parameter c which is the collection to which elements are to be transferred.

Return Value: The function returns the number of elements drained to collection from queue.

Exceptions: This method throws following exceptions:

  • NullPointerException– if the collection is null
  • IllegalArgumentException– if arguments of the method prevents it from being added to the specified collection

Below programs illustrate the use of java.util.concurrent.LinkedTransferQueue.drainTo() method:

Program 1: Program to drain all the elements from the queue to the specified collection.




// Java Program Demonstrate drainTo()
// method of LinkedTransferQueue
  
import java.util.*;
import java.util.concurrent.LinkedTransferQueue;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing the List
        List<Integer> list = new ArrayList<Integer>();
  
        // 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("Elements in the queue = "
                           + queue);
  
        // drainTo() method removes all available elements
        // from this queue and adds them to the list
        queue.drainTo(list);
  
        // Printing the elements of the queue after drainTo()
        System.out.println("Elements left in the queue :"
                           + queue);
  
        // Printing the elements of the list
        System.out.println("Elements drained in the list :"
                           + list);
    }
}


Output:

Elements in the queue = [10, 11, 12, 13, 14, 15]
Elements left in the queue :[]
Elements drained in the list :[10, 11, 12, 13, 14, 15]

Program 2: Program to show the NullPointerException in drainTo().




// Java Program Demonstrate drainTo()
// method of LinkedTransferQueue
  
import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
  
class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // Initializing the queue
        LinkedTransferQueue<Integer>
            queue = new LinkedTransferQueue<Integer>();
  
        // add elements to queue
        queue.put(10);
        queue.put(20);
        queue.put(30);
  
        // create a collection with null
        ArrayList<Integer> add = null;
  
        // try to drain null queue to collection
        try {
            // this will throw exception
            // as the add list is null
            queue.drainTo(add);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

drainTo(Collection c, int maxElements)

The drainTo(Collection c, int maxElements) method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to transfer fixed number elements which is passed as integer in drainTo() to collection which is also passed as parameter to method. After transferring the elements, LinkedTransferQueue has only those elements which are not transferred to collection. This function is same as above function with some limitations to transfer fixed no of element.

Syntax:

public int drainTo(Collection c,
          int maxElements)

Parameter: The method accepts two parameters:

  • c– It represents the collection to transfer elements from LinkedTransferQueue.
  • maxElements– This is of integer type and refers to the maximum number of elements to be transferred to the collection.
  • Return Value: The function returns the number of elements drained to collection from queue.

    Exception: This method throws following exceptions:

    • NullPointerException – if the collection is null
    • IllegalArgumentException – if arguments of the method prevents it from being added to the specified collection

    Program 1: Program to drain at most the given number of available elements from the queue to the specified collection.




    // Java Program Demonstrate drainTo()
    // method of LinkedTransferQueue
      
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.LinkedTransferQueue;
      
    class GFG {
        public static void main(String[] args)
        {
      
            // Initializing the List
            List<Integer> list = new ArrayList<Integer>();
      
            // Initializing the queue
            LinkedTransferQueue<Integer>
                queue = new LinkedTransferQueue<Integer>();
      
            // Adding elements to this queue
            for (int i = 1; i <= 10; i++)
                queue.add(i);
      
            // Printing the elements of the queue
            System.out.println("Elements in the queue = "
                               + queue);
      
            // drainTo() method removes at most
            // the given number of available elements
            // from this queue and adds them to the list.
            queue.drainTo(list, 5);
      
            // Printing the elements of the queue after drainTo()
            System.out.println("Elements left in the queue :"
                               + queue);
      
            // Printing the elements of the list
            System.out.println("Elements drained in the list :"
                               + list);
        }
    }

    
    

    Output:

    Elements in the queue = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Elements left in the queue :[6, 7, 8, 9, 10]
    Elements drained in the list :[1, 2, 3, 4, 5]
    

    Program 2: Program to show the NullPointerException in drainTo().




    // Java Program Demonstrate drainTo()
    // method of LinkedTransferQueue
      
    import java.util.ArrayList;
    import java.util.concurrent.LinkedTransferQueue;
      
    class GFG {
        public static void main(String[] args)
            throws InterruptedException
        {
      
            // Initializing the queue
            LinkedTransferQueue<Integer>
                queue = new LinkedTransferQueue<Integer>();
      
            // add elements to queue
            queue.put(10);
            queue.put(20);
            queue.put(30);
      
            // create a collection with null
            ArrayList<Integer> add = null;
      
            // try to drain null queue to collection
            try {
                // this will throw exception
                // as the add list is null
                queue.drainTo(add, 2);
            }
            catch (Exception e) {
                System.out.println("Exception: " + e);
            }
        }
    }

    
    

    Output:

    Exception: java.lang.NullPointerException
    

    Reference:



    Last Updated : 26 Nov, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads