PriorityBlockingQueue的rainingTo(Collection col)方法从此LinkedBlocking队列中删除所有可用元素,并将它们添加到作为参数传递的给定集合中。
rainingTo(Collection super E> col)
PriorityBlockingQueue的drainTo(Collection super E> col)方法从此队列中删除所有元素,并将它们添加到给定的collection col中。与重复轮询此队列相比,这是一种更有效的方法。
尝试从队列中将元素添加到集合c时,也可能会遇到失败,并且由于该失败,当引发关联的异常时,元素会在两个集合之间分配。如果尝试使用队列将drainTo()自身进行队列,则将抛出IllegalArgumentException。如果在操作进行过程中修改了指定的集合,则此操作的行为是不确定的。因此,使用这种方法时,需要注意这种情况以克服异常。
用法:
public int drainTo(Collection<? super E> col)
参数:此方法接受一个参数col,该参数表示要从PriorityBlockingQueue传输元素的集合。
返回值:此方法返回从队列中耗尽到集合的元素数。
异常:此方法引发以下异常:
- UnsupportedOperationException–如果collection无法添加元素。
- ClassCastException–如果元素类停止将元素添加到集合的方法。
- NullPointerException –如果集合为空
- IllegalArgumentException–如果方法的参数阻止将其添加到指定的集合中
下面的程序说明PriorityBlockingQueue类的drainTo()方法:
示例1:程序,以演示PriorityBlockingQueue上的drainTo()方法,该方法包含ArrayList的数字列表。
// 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<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(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<Integer> list = new ArrayList<Integer>();
// 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);
}
}
No of elements drained :6 ArrayList Contains element 5278367 7855642 35658786 74381793 76487590 87625142
示例2:
下面的程序演示了drainTo()方法引发的异常。
// 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<String> names
= new PriorityBlockingQueue<String>(capacityOfQueue);
// Add names of students of girls college
names.add("Joyita");
names.add("Priyanka");
names.add("Joydeep");
// create a collection with null
ArrayList<String> 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
rainageTo(Collection super E> col,int maxElements)
用于将固定数量的元素(它在drainTo()中作为整数传递)传递给collection(也作为参数传递给方法)的rainingTo(Collection super E> col,int maxElements)。传输元素后,PriorityBlockingQueue仅具有那些未传输到集合的元素。此函数与上述函数相同,但在转移固定元素数量方面存在一些限制。
用法:
public int drainTo(Collection<? super E> col, int maxElements)
参数:该方法接受两个参数:
- col–它表示要从PriorityBlockingQueue传输元素的集合。
- maxElements–这是整数类型,是指要传输到集合的最大元素数。
返回值:该方法返回从队列中耗尽到集合的元素数。
异常:此方法引发以下异常:
- UnsupportedOperationException–如果collection无法添加元素。
- ClassCastException-if元素类的stop方法将元素添加到集合中。
- NullPointerException –如果集合为空
- IllegalArgumentException–如果方法的参数阻止将其添加到指定的集合中
下面的程序说明PriorityBlockingQueue类的排水到(Collection super E> col,int maxElements)方法:
示例1:程序演示PriorityBlockingQueue上的pipeTo(Collection super E> c,int maxElements)方法,该方法包含一个ArrayList的数字列表,而我们仅要传输3个元素。
// 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<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(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<Integer> list = new ArrayList<Integer>();
// 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:
下面的程序演示在名称列表上的rainingTo(Collection super E> c,int maxElements)方法,并将两个名称传输到rraylist并打印该列表。
// 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<String> names
= new PriorityBlockingQueue<String>(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<String> collection = new ArrayList<String>();
// 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/util/concurrent/PriorityBlockingQueue.html#drainTo-java.util.Collection-
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#drainTo-java.util.Collection-int-
相关用法
- Java LinkedBlockingQueue drainTo()用法及代码示例
- Java ArrayBlockingQueue drainTo()用法及代码示例
- Java LinkedTransferQueue drainTo()用法及代码示例
- Java LinkedBlockingDeque drainTo()用法及代码示例
- Java DelayQueue drainTo()用法及代码示例
- Java BlockingQueue drainTo()用法及代码示例
- Java PriorityBlockingQueue take()用法及代码示例
- Java PriorityBlockingQueue contains()用法及代码示例
- Java PriorityBlockingQueue add()用法及代码示例
- Java PriorityBlockingQueue put()用法及代码示例
- Java PriorityBlockingQueue remainingCapacity()用法及代码示例
- Java PriorityBlockingQueue poll()用法及代码示例
- Java PriorityBlockingQueue offer()用法及代码示例
- Java PriorityBlockingQueue size()用法及代码示例
- Java PriorityBlockingQueue spliterator()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue drainTo() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。