LinkedBlockingQueue toString() Method in Java with Examples
The toString() method of LinkedBlockingQueue returns a String representation of the elements of LinkedBlockingQueue. The string of LinkedBlockingQueue contains its elements from first(head) to last(tail), enclosed in square brackets(“[]”) in proper order. The elements are separated by the characters ‘, ‘ (comma and a space). So basically the toString() method is used to convert all the elements of LinkedBlockingQueue into a String representation.
This method overrides the toString() in class AbstractCollection<E>
Syntax:
public String toString()
Return Value: This method returns a String which is the representation of the elements of LinkedBlockingQueue from first(head) to last(tail), enclosed in square brackets(“[]”) in proper order,separated by the ‘, ‘ (comma and a space).
Below programs illustrates toString() method of LinkedBlockingQueue class:
Program 1:
// Java Program Demonstrate toString() // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of LinkedBlockingQueue int capacityOfQueue = 50; // create object of LinkedBlockingQueue LinkedBlockingQueue<Integer> linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue); // Add element to LinkedBlockingQueue linkedQueue.add(2300); linkedQueue.add(1322); linkedQueue.add(8945); linkedQueue.add(6512); // toString() on linkedQueue String queueRepresentation = linkedQueue.toString(); // print results System.out.println("Queue Representation:"); System.out.println(queueRepresentation); } } |
Queue Representation: [2300, 1322, 8945, 6512]
// Java Program Demonstrate toString() // method of LinkedBlockingQueue. import java.util.concurrent.LinkedBlockingQueue; public class GFG { // create an Employee Object with // position and salary as an attribute public class Employee { public String name; public String position; public String salary; Employee(String name, String position, String salary) { this.name = name; this.position = position; this.salary = salary; } @Override public String toString() { return "Employee [name=" + name + ", position=" + position + ", salary=" + salary + "]"; } } // Main Method public static void main(String[] args) { GFG gfg = new GFG(); gfg.stringRepresentation(); } public void stringRepresentation() { // define capacity of LinkedBlockingQueue int capacity = 50; // create object of LinkedBlockingQueue LinkedBlockingQueue<Employee> linkedQueue = new LinkedBlockingQueue<Employee>(capacity); Employee emp1 = new Employee("Aman", "Analyst", "24000"); Employee emp2 = new Employee("Sachin", "Developer", "39000"); // Add Employee Objects to linkedQueue linkedQueue.add(emp1); linkedQueue.add(emp2); // toString() on linkedQueue String queueRepresentation = linkedQueue.toString(); // print results System.out.println("Queue Representation:"); System.out.println(queueRepresentation); } } |
Queue Representation: [Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#toString–
Recommended Posts:
- LinkedBlockingQueue toString() method in Java
- LinkedBlockingQueue put() method in Java with Examples
- LinkedBlockingQueue take() Method in Java with Examples
- Java 8 | LinkedBlockingQueue spliterator() method with Examples
- Byte toString() method in Java with examples
- AtomicLong toString() method in Java with examples
- ChronoZonedDateTime toString() method in Java with Examples
- ZonedDateTime toString() method in Java with Examples
- MonthDay toString() Method in Java with Examples
- DecimalFormatSymbols toString() method in Java with Examples
- LongAccumulator toString() method in Java with Examples
- Optional toString() method in Java with examples
- Currency toString() Method in Java with Examples
- AtomicLongArray toString() method in Java with Examples
- OffsetTime toString() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



