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


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


PriorityBlockingQueue的toString()方法返回此PriorityBlockingQueue的字符串表示形式,其中顯示PriorityBlockingQueue包含的元素的詳細信息。 PriorityBlockingQueue字符串包含由其迭代器返回的順序相同的PriorityBlockingQueue元素,並括在方括號(“[]”)中。元素由字符“,”(逗號和空格)分隔。因此,本質上使用toString()方法將PriorityBlockingQueue的所有元素轉換為單個String。

用法:

public String toString()

返回值:此方法返回一個String,表示該PriorityBlockingQueue的元素。


以下示例程序旨在說明PriorityBlockingQueue的toString()方法:

示例1:為了演示PriorityBlockingQueue上的toString()方法,該方法包含一個整數列表。

// Java Program Demonstrate toString() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<Integer> PrioQueue 
            = new PriorityBlockingQueue<Integer>(); 
  
        // Add numbers to PriorityBlockingQueue 
        PrioQueue.put(45815616); 
        PrioQueue.put(4981561); 
        PrioQueue.put(4594591); 
        PrioQueue.put(9459156); 
  
        // get String representation of PriorityBlockingQueue 
        String str = PrioQueue.toString(); 
  
        // print Details 
        System.out.println("String representation: " + str); 
    } 
}
輸出:
String representation: [4594591, 9459156, 4981561, 45815616]

示例2:演示PriorityBlockingQueue上的toString()方法,該方法包含字符串列表

// Java Program Demonstrate toString() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of PriorityBlockingQueue 
        // which contains Strings 
        PriorityBlockingQueue<String> names 
            = new PriorityBlockingQueue<String>(); 
  
        // Add Strings 
        names.add("Geeks"); 
        names.add("forGeeks"); 
        names.add("A Computer Portal"); 
        names.add("By Sandeep Jain"); 
  
        // get String representation of PriorityBlockingQueue 
        String str = names.toString(); 
  
        // print Details 
        System.out.println("String representation: " + str); 
    } 
}
輸出:
String representation: [A Computer Portal, By Sandeep Jain, Geeks, forGeeks]

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



相關用法


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