📌  相关文章
📜  Java中的PriorityBlockingQueue drainTo() 方法

📅  最后修改于: 2022-05-13 01:55:52.045000             🧑  作者: Mango

Java中的PriorityBlockingQueue drainTo() 方法

PriorityBlockingQueue 的drainTo(Collection col)方法从此 LinkedBlocking Queue 中删除所有可用元素,并将它们添加到作为参数传递的给定集合中。

drainTo(Collection col)

PriorityBlockingQueue 的drainTo(Collection col)方法从该队列中移除所有元素并将它们添加到给定的集合 col。这是比重复轮询此队列更有效的方法。
在尝试将元素从队列中添加到集合 c 时,也有可能遇到失败,并且由于该失败,当引发关联的异常时,元素会在两个集合之间分布。如果一个队列试图通过 drainTo() 来排队,那么 IllegalArgumentException 将被抛出。如果在操作进行时修改了指定的集合,则此操作的行为是未定义的。因此,对于使用此类方法,需要注意这种情况以克服异常。
句法:

public int drainTo(Collection col)

参数:此方法接受一个参数col ,该参数表示要从PriorityBlockingQueue 传输元素的集合。
返回值:此方法返回从队列中排出到集合的元素数。
异常:此方法抛出以下异常:

  • UnsupportedOperationException – 如果集合无法添加元素。
  • ClassCastException – 如果元素的类停止将元素添加到集合的方法。
  • NullPointerException – 如果集合为空
  • IllegalArgumentException – 如果方法的参数阻止它被添加到指定的集合中

下面的程序说明了 PriorityBlockingQueue 类的 drainTo() 方法:
示例 1:演示 PriorityBlockingQueue 上的 drainTo() 方法的程序,其中包含 ArrayList 的数字列表。

Java
// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add numbers to PriorityBlockingQueue
        PrioQueue.put(7855642);
        PrioQueue.put(35658786);
        PrioQueue.put(5278367);
        PrioQueue.put(74381793);
        PrioQueue.put(76487590);
        PrioQueue.put(87625142);
 
        // create a ArrayList of Integers
        ArrayList list = new ArrayList();
 
        // drain all elements of PriorityBlockingQueue to ArrayList
        // Using drainTo() method
        int noOfElementDrained = PrioQueue.drainTo(list);
 
        // print details
        System.out.println("No of elements drained :" + noOfElementDrained);
        System.out.println("ArrayList Contains element");
 
        for (int i : list)
            System.out.println(i);
    }
}


Java
// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        names.add("Joyita");
        names.add("Priyanka");
        names.add("Joydeep");
 
        // create a collection with null
        ArrayList collection = null;
 
        // try to drain to collection equals to null
        try {
            names.drainTo(collection);
        }
        catch (Exception e) {
            System.out.println("Exception Thrown: " + e);
        }
    }
}


Java
// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add numbers to PriorityBlockingQueue
        PrioQueue.put(7855642);
        PrioQueue.put(35658786);
        PrioQueue.put(5278367);
        PrioQueue.put(74381793);
        PrioQueue.put(76487590);
        PrioQueue.put(87625142);
 
        // create maxElement variable
        // Contains no of elements we want to transfer
        int maxElement = 3;
 
        // create a ArrayList of Integers
        ArrayList list = new ArrayList();
 
        // drain all elements of PriorityBlockingQueue to ArrayList
        // Using drainTo() method
        int noOfElementDrained = PrioQueue.drainTo(list, maxElement);
 
        // print details
        System.out.println("ArrayList Contains element");
 
        for (int i : list)
            System.out.println(i);
    }
}


Java
// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        names.add("Joyita");
        names.add("Priyanka");
        names.add("Ravi");
        names.add("Suraj");
 
        // create maxElement variable
        // Contains no of elements we want to transfer
        // here we want to transfer only two names
        int maxElement = 2;
 
        // create a collection with null
        ArrayList collection = new ArrayList();
 
        // drain all elements of PriorityBlockingQueue to ArrayList
        // Using drainTo() method
        int noOfElementDrained = names.drainTo(collection, maxElement);
 
        // print details
        System.out.println("ArrayList Contains element");
 
        int i = 0;
        for (String str : collection) {
            System.out.println("Element " + i + " : " + str);
            i++;
        }
    }
}


输出:
No of elements drained :6
ArrayList Contains element
5278367
7855642
35658786
74381793
76487590
87625142

示例 2:
下面的程序演示了 drainTo() 方法抛出的异常。

Java

// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        names.add("Joyita");
        names.add("Priyanka");
        names.add("Joydeep");
 
        // create a collection with null
        ArrayList collection = null;
 
        // try to drain to collection equals to null
        try {
            names.drainTo(collection);
        }
        catch (Exception e) {
            System.out.println("Exception Thrown: " + e);
        }
    }
}
输出:
Exception Thrown: java.lang.NullPointerException

drainTo(Collection col, int maxElements)

drainTo(Collection col, int maxElements)用于将固定数量的元素传递给在 drainTo() 中作为整数传递的集合,该集合也作为参数传递给方法。转移元素后,PriorityBlockingQueue 只有那些没有转移到集合的元素。此函数与上述函数相同,但对于传输固定数量的元素有一些限制。
句法:

public int drainTo(Collection col, int maxElements)

参数:该方法接受两个参数:

  • col - 它表示从 PriorityBlockingQueue 传输元素的集合。
  • maxElements – 这是整数类型,是指要传输到集合的最大元素数。

返回值:该方法返回从队列中排出到集合的元素数
异常:此方法抛出以下异常:

  • UnsupportedOperationException – 如果集合无法添加元素。
  • ClassCastException - 如果元素类停止将元素添加到集合的方法。
  • NullPointerException – 如果集合为空
  • IllegalArgumentException – 如果方法的参数阻止它被添加到指定的集合中

下面的程序说明了 PriorityBlockingQueue 类的 drainTo(Collection col, int maxElements) 方法:
示例 1:演示 PriorityBlockingQueue 上的 drainTo(Collection c, int maxElements) 方法的程序,该方法包含一个到 ArrayList 的数字列表,我们只传输 3 个元素。

Java

// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add numbers to PriorityBlockingQueue
        PrioQueue.put(7855642);
        PrioQueue.put(35658786);
        PrioQueue.put(5278367);
        PrioQueue.put(74381793);
        PrioQueue.put(76487590);
        PrioQueue.put(87625142);
 
        // create maxElement variable
        // Contains no of elements we want to transfer
        int maxElement = 3;
 
        // create a ArrayList of Integers
        ArrayList list = new ArrayList();
 
        // drain all elements of PriorityBlockingQueue to ArrayList
        // Using drainTo() method
        int noOfElementDrained = PrioQueue.drainTo(list, maxElement);
 
        // print details
        System.out.println("ArrayList Contains element");
 
        for (int i : list)
            System.out.println(i);
    }
}
输出:
ArrayList Contains element
5278367
7855642
35658786

示例 2:
下面的程序演示了名称列表上的 drainTo(Collection c, int maxElements) 方法,并将两个名称传输到 arraylist 并打印列表。

Java

// Java Program Demonstrate drainTo()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
 
        // Add names of students of girls college
        names.add("Joyita");
        names.add("Priyanka");
        names.add("Ravi");
        names.add("Suraj");
 
        // create maxElement variable
        // Contains no of elements we want to transfer
        // here we want to transfer only two names
        int maxElement = 2;
 
        // create a collection with null
        ArrayList collection = new ArrayList();
 
        // drain all elements of PriorityBlockingQueue to ArrayList
        // Using drainTo() method
        int noOfElementDrained = names.drainTo(collection, maxElement);
 
        // print details
        System.out.println("ArrayList Contains element");
 
        int i = 0;
        for (String str : collection) {
            System.out.println("Element " + i + " : " + str);
            i++;
        }
    }
}
输出:
ArrayList Contains element
Element 0 : Joyita
Element 1 : Priyanka

参考:

  • https://docs.oracle.com/javase/8/docs/api/java Java
  • https://docs.oracle.com/javase/8/docs/api/java Java