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


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