德兰托(c系列)
这个 德兰托(c系列) 方法 JAVAutil。同时发生的链接传输队列 类是Java中的一个内置函数,它删除此队列中的所有元素,并将它们添加到提供的集合中。这是一种比重复轮询此队列更有效的方法。
在尝试从队列向集合c添加元素时,也可能会遇到失败,并且由于该失败,当引发关联的异常时,元素会分布在两个集合之间。如果一个队列试图拖入()来排队,那么 非法数据异常 将被抛出。如果在操作进行期间修改了指定的集合,则此操作的行为未定义。因此,使用这些方法时,需要注意这种情况,以克服例外情况。
语法:
public int drainTo(Collection c)
参数: 该函数接受一个强制参数 C 这是元素要传输到的集合。
返回值: 函数返回从队列中排入集合的元素数。
例外情况: 此方法引发以下异常:
- 空指针异常 –如果集合为空
- 非法数据异常 –如果方法的参数阻止将其添加到指定集合中
下面的程序演示了java的使用。util。同时发生的LinkedTransferQueue。drainTo()方法:
项目1: 程序将队列中的所有元素排入指定集合。
// 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); } } |
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]
项目2: 在drainTo()中显示NullPointerException的程序。
// 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); } } } |
Exception: java.lang.NullPointerException
drainTo(集合c,int maxElements)
这个 drainTo(集合c,int maxElements) 方法 JAVAutil。同时发生的链接传输队列 是Java中的一个内置函数,用于将固定数元素作为整数在drainTo()中传递给集合,集合也作为参数传递给方法。传输元素后,LinkedTransferQueue只包含那些未传输到集合的元素。该函数与上述函数相同,但在传递固定数量的元素时存在一些限制。
语法:
public int drainTo(Collection c, int maxElements)
参数: 该方法接受两个参数:
- C –它表示从LinkedTransferQueue传输元素的集合。
- 最大元素 –这是整数类型,是指要传输到集合的最大元素数。
返回值: 函数返回从队列中排入集合的元素数。
例外情况: 此方法引发以下异常:
- 空指针异常 –如果集合为空
- 非法数据异常 –如果方法的参数阻止将其添加到指定集合中
项目1: 程序将队列中最多给定数量的可用元素排入指定集合。
// 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);
}
}
输出: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]
项目2: 在drainTo()中显示NullPointerException的程序。
// 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);
}
}
}
输出:Exception: java.lang.NullPointerException
参考:
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#drainTo(java.util.Collection)
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#drainTo(java.util.Collection,%20int)