LinkedTransferQueue drainTo() method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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().

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    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().

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
            }
        }
    }

    chevron_right

    
    

    Output:

    Exception: java.lang.NullPointerException
    

    Reference:



    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.