Skip to content
Related Articles

Related Articles

Improve Article

PriorityBlockingQueue toString() method in Java

  • Last Updated : 26 Nov, 2018

The toString() method of PriorityBlockingQueue returns String representation of this PriorityBlockingQueue showing details of element contained by PriorityBlockingQueue. The string of PriorityBlockingQueue contains elements of PriorityBlockingQueue in the same order returned by its iterator, enclosed in square brackets(“[]”). The elements are separated by the characters ”, ” (comma and a space). So basically the toString() method is used to convert all the elements of PriorityBlockingQueue into a single String.

Syntax:

public String toString()

Returns: This method returns a String representing the elements of this PriorityBlockingQueue.

Below programs illustrate illustrate toString() method of PriorityBlockingQueue:

Example 1: To demonstrate toString() method on PriorityBlockingQueue which contains a list of Integers.






// 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);
    }
}
Output:
String representation: [4594591, 9459156, 4981561, 45815616]

Example 2: To demonstrate toString() method on PriorityBlockingQueue which contains list of Strings




// 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);
    }
}
Output:
String representation: [A Computer Portal, By Sandeep Jain, Geeks, forGeeks]

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :