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


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


PriorityBlockingQueue的peek()方法返回PriorityBlockingQueue開頭的元素。它檢索LinkedBlockingQueue頭的值,但不刪除它。如果PriorityBlockingQueue不包含任何元素,則此方法返回null。 PriorityBlockingQueue隊列使用與類PriorityQueue相同的排序規則。

用法:

public E peek()

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


返回值:此方法返回PriorityBlockingQueue的頭部。

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

示例1:

// Java Program Demonstrate peek() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
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(464161); 
        PrioQueue.add(416165); 
  
        // print PrioQueue 
        System.out.println("PrioQueue: " + PrioQueue); 
  
        // get head of PriorityBlockingQueue 
        int head = PrioQueue.peek(); 
  
        // print head of PriorityBlockingQueue 
        System.out.println("Head of Queue: " + head); 
    } 
}
輸出:
PrioQueue: [416165, 464161]
Head of Queue: 416165

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

// Java Program Demonstrate peek() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
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"); 
  
        // print PrioQueue 
        System.out.println("List of Names: " + names.toString()); 
  
        // get head of PriorityBlockingQueue 
        String head = names.peek(); 
  
        // print head of PriorityBlockingQueue 
        System.out.println("Head of Queue: " + head); 
  
        // remove one name from head 
        names.poll(); 
        System.out.println("First Name from head is removed"); 
  
        // print PrioQueue 
        System.out.println("List of Names: " + names.toString()); 
  
        // get head of PriorityBlockingQueue 
        head = names.peek(); 
  
        // print head of PriorityBlockingQueue 
        System.out.println("Head of Queue: " + head); 
    } 
}
輸出:
List of Names: [A, Computer, Geeks, forGeeks, Portal]
Head of Queue: A
First Name from head is removed
List of Names: [Computer, Portal, Geeks, forGeeks]
Head of Queue: Computer

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



相關用法


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