VOOZH about

URL: https://www.geeksforgeeks.org/java/linkedblockingqueue-remove-method-in-java/

⇱ LinkedBlockingQueue remove() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LinkedBlockingQueue remove() method in Java

Last Updated : 5 Apr, 2023

The remove(Object obj) method of LinkedBlockingQueue removes only one instance of the given Object, passed as parameter, from this LinkedBlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns true if this queue contained the element which is now removed from LinkedBlockingQueue.

Syntax:

public boolean remove(Object o)

Parameter: This method accepts a mandatory parameter obj which is the element to be removed from LinkedBlockingQueue. 

Return Value: This method returns true if this queue contained the element which is now removed from LinkedBlockingQueue. If the LinkedBlockingQueue did not contain the element obj, then this method returns false

Below programs illustrates remove(Object obj) method of LinkedBlockingQueue class: 

Program 1: Try to remove some element from LinkedBlockingQueue using remove(Object obj) and print result. 

Output:
Items in Queue are [Karan, Suraj, Harsh, Rahul]
String name Karan is removed :true
String name Sunny is removed :false
String name Harsh is removed :false
After Removing Some Elements:
Items in Queue are [Suraj, Rahul]

Program 1: Remove Employee object using remove(Object obj) method in LinkedBlockingQueue 

Output:
Before removing Elements
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 removing Some Elements
Employee [name=Karan, position=Analyst, salary=44000, Age=30]

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#remove-java.lang.Object-

Comment