LinkedBlockingDeque的drainTo(Collection col)方法從此LinkedBlockingDeque中刪除所有可用元素,並將它們添加到作為參數傳遞的給定集合中。
rainTo(Collection col)
LinkedBlockingDeque的drainTo(Collection col)方法從此雙端隊列中刪除所有元素,並將它們添加到給定的collect col中。與重複輪詢此雙端隊列相比,這是一種更有效的方法。
嘗試從雙端隊列將元素添加到集合c時,也可能會遇到失敗,並且由於該失敗,當引發關聯的異常時,元素會在兩個集合之間分配。如果嘗試使用雙端隊列使drainTo()自身進行雙端隊列,則將拋出IllegalArgumentException。如果在操作進行過程中修改了指定的集合,則此操作的行為是不確定的。因此,使用這種方法時,需要注意這種情況以克服異常。
用法:
public int drainTo(Collection<? super E> col)
參數:此方法接受一個參數col,該參數表示要從LinkedBlockingDeque傳輸元素的集合。
返回值:此方法返回從雙端隊列耗盡到集合的元素數。
異常:此方法引發以下異常:
- UnsupportedOperationException–如果collection無法添加元素。
- ClassCastException–如果元素類停止將元素添加到集合的方法。
- NullPointerException –如果集合為空
- IllegalArgumentException–如果方法的參數阻止將其添加到指定的集合中
以下示例程序旨在說明LinkedBlockingDeque類的drainTo()方法:
示例1:
下麵的程序有一個存儲員工對象的LinkedBlockingDeque。有一個ArrayList,它將存儲LinkedBlockingDeque中的所有雇員對象。因此,將drainTo()與LinkedBlockingDeque一起使用,將所有員工從雙端隊列傳遞到ArrayList。
// Java Program Demonstrate drainTo(Collection c)
// method of LinkedBlockingDeque.
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
// create a Employee Object with
// position and salary as an attribute
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", position="
+ position + ", salary=" + salary + "]";
}
}
// Main Method
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
// define capacity of LinkedBlockingDeque
int capacity = 50;
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Employee> linkedDeque
= new LinkedBlockingDeque<Employee>(capacity);
// create a ArrayList to pass as parameter to drainTo()
ArrayList<Employee> collection
= new ArrayList<Employee>();
// add Employee object to deque
Employee emp1 = new Employee("Aman", "Analyst", "24000");
Employee emp2 = new Employee("Sachin", "Developer", "39000");
linkedDeque.add(emp1);
linkedDeque.add(emp2);
// printing Arraylist and deque
System.out.println("Before drainTo():");
System.out.println("LinkedBlockingDeque : \n"
+ linkedDeque.toString());
System.out.println("ArrayList : \n"
+ collection);
// Apply drainTo method and pass collection as parameter
int response = linkedDeque.drainTo(collection);
// print no of element passed
System.out.println("\nNo of element passed: "
+ response);
// printing Arraylist and deque
// after applying drainTo() method
System.out.println("\nAfter drainTo():");
System.out.println("LinkedBlockingDeque : \n"
+ linkedDeque.toString());
System.out.println("ArrayList : \n"
+ collection);
}
}
Before drainTo(): LinkedBlockingDeque : [Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]] ArrayList : [] No of element passed: 2 After drainTo(): LinkedBlockingDeque : [] ArrayList : [Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]]
示例2:該程序顯示drainTo()方法引發的異常。
// Java Program Demonstrate
// drainTo(Collection C)
// method of LinkedBlockingDeque.
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingDeque
int capacityOfDeque = 4;
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Integer> linkedDeque
= new LinkedBlockingDeque<Integer>(capacityOfDeque);
// add elements to deque
linkedDeque.put(85461);
linkedDeque.put(44648);
linkedDeque.put(45654);
// create a collection with null
ArrayList<Integer> add = null;
// try to drain null deque to collection
try {
linkedDeque.drainTo(add);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Exception: java.lang.NullPointerException
rainageTo(Collection super E> col,int maxElements)
用於將固定數量的元素(它在drainTo()中作為整數傳遞)傳遞給collection(也作為參數傳遞給方法)的rainingTo(Collection super E> col,int maxElements)。傳輸元素後,LinkedBlockingDeque僅具有那些未傳輸到集合的元素。此函數與上述函數相同,但在轉移固定元素數量方麵存在一些限製。
用法:
public int drainTo(Collection<E> col, int maxElements)
參數:該方法接受兩個參數:
- col–它表示從LinkedBlockingDeque傳輸元素的集合。
- maxElements–這是整數類型,是指要傳輸到集合的最大元素數。
返回值:該方法返回從雙端隊列耗盡到集合的元素數。
異常:此方法引發以下異常:
- UnsupportedOperationException–如果collection無法添加元素。
- ClassCastException-if元素類的stop方法將元素添加到集合中。
- NullPointerException –如果集合為空
- IllegalArgumentException–如果方法的參數阻止將其添加到指定的集合中
以下示例程序旨在說明LinkedBlockingDeque類的排水到(收集超級E> col,int maxElements)方法
示例1:
下麵的程序有一個LinkedBlockingDeque,它存儲Employee對象,還有一個HashSet,它將存儲LinkedBlockingDeque中的所有雇員對象。因此,LinkedBlockingDeque的drainTo()用於將某些雇員從雙端隊列傳遞到ArrayList。因此,沒有要傳遞的元素作為參數傳遞給方法。
// Java program to demonstrate drainTo()
// method of LinkedBlockingDeque.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
// create an Employee Object with
// position and salary as attribute
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", "
+ "position=" + position
+ ", salary=" + salary + "]";
}
}
// Main Method
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
// define capacity of LinkedBlockingDeque
int capacity = 10;
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Employee> linkedDeque
= new LinkedBlockingDeque<Employee>(capacity);
// create a HashSet to pass as parameter to drainTo()
HashSet<Employee> collection
= new HashSet<Employee>();
// add Employee object to deque
Employee emp1 = new Employee("Sachin",
"Analyst",
"40000");
Employee emp2 = new Employee("Aman",
"Developer",
"69000");
Employee emp3 = new Employee("Kajal",
"Accountant",
"39000");
linkedDeque.add(emp1);
linkedDeque.add(emp2);
linkedDeque.add(emp3);
// printing Arraylist and deque
// before applying drainTo() method
System.out.println("Before drainTo():");
System.out.println("No of Elements in Deque is "
+ linkedDeque.size());
System.out.println("Elements in Deque is as follows");
Iterator<Employee> listOfemp
= linkedDeque.iterator();
while (listOfemp.hasNext())
System.out.println(listOfemp.next());
System.out.println("No of Elements in HashSet is "
+ collection.size());
System.out.println("Elements in HashSet is as follows:");
for (Employee emp : collection)
System.out.println(emp);
// Initialize no of element passed to collection
// using drainTo() method
int noOfElement = 2;
// Apply drainTo method
// and pass collection as parameter
int response
= linkedDeque.drainTo(collection, noOfElement);
// print no of element passed
System.out.println("\nNo of element passed: "
+ response);
// printing Arraylist and deque
// after applying drainTo() method
System.out.println("\nAfter drainTo():");
System.out.println("No of Elements in Deque is "
+ linkedDeque.size());
System.out.println("Elements in Deque is as follows");
listOfemp = linkedDeque.iterator();
while (listOfemp.hasNext())
System.out.println(listOfemp.next());
System.out.println("No of Elements in HashSet is "
+ collection.size());
System.out.println("Elements in HashSet is as follows:");
for (Employee emp : collection)
System.out.println(emp);
}
}
Before drainTo(): No of Elements in Deque is 3 Elements in Deque is as follows Employee [name=Sachin, position=Analyst, salary=40000] Employee [name=Aman, position=Developer, salary=69000] Employee [name=Kajal, position=Accountant, salary=39000] No of Elements in HashSet is 0 Elements in HashSet is as follows: No of element passed: 2 After drainTo(): No of Elements in Deque is 1 Elements in Deque is as follows Employee [name=Kajal, position=Accountant, salary=39000] No of Elements in HashSet is 2 Elements in HashSet is as follows: Employee [name=Sachin, position=Analyst, salary=40000] Employee [name=Aman, position=Developer, salary=69000]
相關用法
- Java ArrayBlockingQueue drainTo()用法及代碼示例
- Java LinkedBlockingQueue drainTo()用法及代碼示例
- Java PriorityBlockingQueue drainTo()用法及代碼示例
- Java LinkedTransferQueue drainTo()用法及代碼示例
- Java DelayQueue drainTo()用法及代碼示例
- Java BlockingQueue drainTo()用法及代碼示例
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque put()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque remainingCapacity()用法及代碼示例
- Java LinkedBlockingDeque addLast()用法及代碼示例
- Java LinkedBlockingDeque putLast()用法及代碼示例
- Java LinkedBlockingDeque element()用法及代碼示例
注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 LinkedBlockingDeque drainTo() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。