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.
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.
String representation: [4594591, 9459156, 4981561, 45815616]
Example 2: To demonstrate toString() method on PriorityBlockingQueue which contains list of Strings
String representation: [A Computer Portal, By Sandeep Jain, Geeks, forGeeks]