Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

LinkedBlockingQueue size() method in Java

Last Updated on March 10, 2022 by Ria Pathak

Introduction

The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

In this article, we are going to learn how to use the LinkedBlockingQueue size() method in Java.

LinkedBlockingQueue is a optionally bounded blocking queue based upon linked nodes. It follows FIFO, means that the oldest inserted element will be at the head of the queue. If capacity is given LinkedBlockingQueue is bounded otherwise it is unbounded.

In this question, we are given a LinkedBlockingQueue. We have to implement the LinkedBlockingQueue size() method.

LinkedBlockingQueue size() method

Syntax – public int size()

Return Value – Returns the number of elements present in the LinkedBlockingQueue.

Suppose the LinkedBlockingQueue is [“Coding”,”is,”fun”]. As there are 3 elements in the LinkedBlockingQueue, the size method will return the value 3.

Input: [“Coding”,”is,”fun”]
Output
Items in the Queue are [Coding, is, fun]
Size of the Queue is 3

Explanation: As there are 3 elements in the LinkedBlockingQueue, the size() method returns 3.

Code Implementation

import java.util.concurrent.LinkedBlockingQueue;
  
public class PrepBytes {
  
    public static void main(String[] args)
        throws InterruptedException
    {

        int capacityOfQueue = 3;

        LinkedBlockingQueue LQueue
            = new LinkedBlockingQueue(capacityOfQueue);

        LQueue.put("Coding");
        LQueue.put("is");
        LQueue.put("fun");
        int size = LQueue.size();

        size = LQueue.size();
  
        System.out.println("Items in the Queue are " + LQueue);
        System.out.println("Size of the Queue is " + size);
    }
}

Output

Items in the Queue are [Coding, is, fun]
Size of the Queue is 3

Time Complexity: O(1), as all the LinkedBlockingQueue methods other than Remove() operate in constant time.

[forminator_quiz id=”4214″]

So, in this article, we have tried to explain how to use LinkedBlockingQueue size() method in Java. This is an important concept when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Leave a Reply

Your email address will not be published. Required fields are marked *