当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。