當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java PriorityBlockingQueue drainTo()用法及代碼示例


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

參考:



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 PriorityBlockingQueue drainTo() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。