📌  相关文章
📜  Java中的 PriorityBlockingQueue iterator() 方法

📅  最后修改于: 2022-05-13 01:54:36.345000             🧑  作者: Mango

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 PrioQueue
            = new PriorityBlockingQueue(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 names
            = new PriorityBlockingQueue(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 Java