LinkedBlockingQueue size() method in Java
The size() method of LinkedBlockingQueue returns the number of elements that LinkedBlockingQueue contains. When queue is empty queue, size() returns 0 and when queue is full then size() returns the capacity of queue.
public int size()
Return Value: This method returns the number of elements in the LinkedBlockingQueue at that instant. The return type is int.
Below programs illustrates size() method of LinkedBlockingQueue class:
Program 1: Perform add and remove operation on LinkedBlockingQueue an after each operation print size of queue using size()
Items in Queue are [Karan, Suraj] Size of Queue is 2 Items in Queue are [Karan, Suraj, Harsh, Rahul] Size of Queue is 4 String name Karan is removed :true Items in Queue are [Suraj, Harsh, Rahul] Size of Queue is 3
Program 2: Perform add and remove operation on LinkedBlockingQueue, containing Employee objects, an after each operation print size of queue using size()
Size of Queue 0 After Adding some Elements queue has Employee [name=Ranjeet, position=Tester, salary=29000, Age=27] Employee [name=Sanjeet, position=Manager, salary=98000, Age=34] Employee [name=Karan, position=Analyst, salary=44000, Age=30] After Adding Size of Queue 3 After removing Some Elements Employee [name=Karan, position=Analyst, salary=44000, Age=30] After removing size of Queue 1
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.