Wednesday, December 24, 2025
HomeLanguagesJavaArrayBlockingQueue toString() Method in Java

ArrayBlockingQueue toString() Method in Java

The toString() method of ArrayBlockingQueue class is used to get a string representation of the objects of ArrayBlockingQueue. The string of ArrayBlockingQueue contains elements of ArrayBlockingQueue in the order from first(head) to last(tail), 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 ArrayBlockingQueue into a String.

Syntax:

public String toString()

Return Value: The method returns an String representation of ArrayBlockingQueue.

Below programs illustrates toString() method of ArrayBlockingQueue class:

Program 1:




// Program to demonstrate how to apply toString() method
// of ArrayBlockingQueue Class.
  
import java.util.concurrent.ArrayBlockingQueue;
  
  
public class GFG {
  
public static void main(String[] args) {
    // Define capacity of ArrayBlockingQueue
    int capacity = 5;
      
    // Create object of ArrayBlockingQueue
    ArrayBlockingQueue<Integer> queue = 
        new ArrayBlockingQueue<Integer>(capacity);
      
    // Add 5 elements to ArrayBlockingQueue
    queue.offer(423);
    queue.offer(422);
    queue.offer(421);
    queue.offer(420);
    queue.offer(424);
      
    // Print queue
    System.out.println("Queue is "+queue);
      
    // Call toString() method and Create an iterator
    String stringRepresentation=queue.toString();
      
    // Print String value returned by toString() method
    System.out.println("\nThe String returned by toString():");
    System.out.println(stringRepresentation);
      
      
    
}


Output:

Queue is [423, 422, 421, 420, 424]

The String returned by toString():
[423, 422, 421, 420, 424]

Program 2:




// Program Demonstrate how to apply toString() method
// of ArrayBlockingQueue Class.
  
import java.util.concurrent.ArrayBlockingQueue;
  
  
public class GFG {
  
public static void main(String[] args) {
    // Define capacity of ArrayBlockingQueue
    int capacity = 10;
      
    // Create object of ArrayBlockingQueue
    ArrayBlockingQueue<String> queue = 
                    new ArrayBlockingQueue<String>(capacity);
        
    // Add 5 elements to ArrayBlockingQueue
    queue.offer("User");
    queue.offer("Employee");
    queue.offer("Manager");
    queue.offer("Analyst");
    queue.offer("HR");
    queue.offer("Tester");
      
    // Print queue
    System.out.println("Queue is "+queue);
      
    // Call toString() method and Create an iterator
    String stringRepresentation=queue.toString();
      
    // Print String value returned by toString() method
    System.out.println("\nThe String returned by toString():");
    System.out.println(stringRepresentation);
      
      
    
}


Output:

Queue is [User, Employee, Manager, Analyst, HR, Tester]

The String returned by toString():
[User, Employee, Manager, Analyst, HR, Tester]

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

RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11960 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6959 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6893 POSTS0 COMMENTS