Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

tostring
java
communitycreator

What is the ConcurrentLinkedQueue.toString() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ConcurrentLinkedQueue is a thread-safe unbounded queue. The elements are ordered by FIFOFirst-In-First-Out. The elements are inserted at the tail (end) and retrieved from the head (start) of the queue. The null elements are not allowed as an element of the queue. We can use ConcurrentLinkedQueue when multiple threads are sharing a single Queue.

The toString method will return the String representation of the ConcurrentLinkedQueue object with the String representation of each element in the queue.

Syntax

public String toString()

This method doesn’t take any argument.

This method returns String as result. The returned string contains the elements of the queue in the insertion order enclosed between [] and separated by comma(,). Internally, the elements are converted to a string using the String.valueOf(Object) method.

Code

import java.util.concurrent.ConcurrentLinkedQueue;
class ToString {
public static void main( String args[] ) {
ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
queue.add(1);
queue.add(2);
queue.add(3);
System.out.println(queue.toString());
}
}

In the above code:

  • In line number 1, we imported the ConcurrentLinkedQueue class.

  • In line number 4, we created a new ConcurrentLinkedQueue object with the name queue.

  • From line number 5 to 7, we added three elements (1,2,3) to the queue object using the add method.

  • In line number 8, we used the toString method to get the string representation of the queue object.

RELATED TAGS

tostring
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring