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


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


PriorityBlockingQueue類的iterator()方法返回對該隊列中的元素進行迭代的迭代器。從此方法返回的元素不遵循任何順序。返回的迭代器是弱一致性的。

用法:

public Iterator iterator()

參數:此方法不帶任何參數。


返回:此方法返回一個迭代器,該迭代器具有與PriorityBlockingQueue中存在的元素相同的元素。

下麵的程序說明PriorityBlockingQueue的iterator()方法。

範例1:

// Java Program Demonstrate iterator() 
// 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 elements to PriorityBlockingQueue 
        PrioQueue.add(945645); 
        PrioQueue.add(6468516); 
        PrioQueue.add(7564165); 
        PrioQueue.add(45616); 
  
        // print PrioQueue 
        System.out.println("PrioQueue:" + PrioQueue); 
  
        // Call iterator() method of PriorityBlockingQueue 
        Iterator iteratorVals = PrioQueue.iterator(); 
  
        // Print elements of iterator 
        // created from PriorityBlockingQueue 
        System.out.println("The iterator values"
                           + " of PriorityBlockingQueue are:"); 
  
        while (iteratorVals.hasNext()) { 
            System.out.println(iteratorVals.next()); 
        } 
    } 
}
輸出:
PrioQueue:[45616, 945645, 7564165, 6468516]
The iterator values of PriorityBlockingQueue are:
45616
945645
7564165
6468516

範例2:為了說明PriorityBlockingQueue的iterator()方法,該方法包含名稱列表。

// Java Program Demonstrate iterator() 
// 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<String> names 
            = new PriorityBlockingQueue<String>(capacityOfQueue); 
  
        // Add names of students of girls college 
        names.add("Geeks"); 
        names.add("forGeeks"); 
        names.add("A"); 
        names.add("Computer"); 
        names.add("Portal"); 
  
        // Call iterator() method of PriorityBlockingQueue 
        Iterator iteratorVals = names.iterator(); 
  
        // Print elements of iterator 
        // created from PriorityBlockingQueue 
        System.out.println("The Names are:"); 
  
        while (iteratorVals.hasNext()) { 
            System.out.println(iteratorVals.next()); 
        } 
    } 
}
輸出:
The Names are:
A
Computer
Geeks
forGeeks
Portal

相關文章:

參考:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#iterator-



相關用法


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