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


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

drainTo(Collection super E> c)

java.util.concurrent.LinkedTransferQueue類的raintTo(Collection c)方法是Java中的內置函數,它刪除此隊列中存在的所有元素並將它們添加到提供的集合中。與重複輪詢此隊列相比,這是一種更有效的方法。

嘗試從隊列中將元素添加到集合c時,也可能會遇到失敗,並且由於該失敗,當引發關聯的異常時,元素會在兩個集合之間分配。如果嘗試使用隊列將drainTo()自身進行隊列,則將拋出IllegalArgumentException。如果在操作進行過程中修改了指定的集合,則此操作的行為是不確定的。因此,使用這種方法時,需要注意這種情況以克服異常。

用法:


public int drainTo(Collection super E> c)

參數:該函數接受強製性參數c,該參數是要將元素傳輸到的集合。

返回值:該函數返回從隊列中耗盡到集合的元素數。

異常:此方法引發以下異常:

  • NullPointerException -如果集合為null
  • IllegalArgumentException-如果方法的參數阻止將其添加到指定的集合中

以下程序說明了java.util.concurrent.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(Collection super E> c, int maxElements)

java.util.concurrent.LinkedTransferQueue的drainTo(Collection super E> c,int maxElements)方法是Java中的一個內置函數,用於將固定數字元素(在drainTo()中作為整數傳遞)傳遞給也傳遞的集合作為方法的參數。傳輸元素後,LinkedTransferQueue僅具有那些未傳輸到集合的元素。此函數與上述函數相同,但在轉移固定元素數量方麵存在一些限製。

用法:


public int drainTo(Collection super E> c,
          int maxElements)

參數:該方法接受兩個參數:

  • c-它表示從LinkedTransferQueue傳輸元素的集合。
  • maxElements-這是整數類型,是指要轉移到集合中的最大元素數。
  • 返回值:該函數返回從隊列中耗盡到集合的元素數。

    Exception:此方法引發以下異常:

    • NullPointerException -如果集合為null
    • IllegalArgumentException-如果方法的參數阻止將其添加到指定的集合中

    程序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
    

    Reference:



相關用法


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