BlockingQueue remove() method in Java with examples (original) (raw)
Last Updated : 14 Oct, 2019
The remove(Object obj) method of BlockingQueue removes only one instance of the given Object, passed as parameter, from this BlockingQueue 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 BlockingQueue.
Syntax:
public boolean remove(Object o)
Parameter: This method accepts a mandatory parameter obj which is the element to be removed from BlockingQueue.
Return Value: This method returns true if this queue contained the element which is now removed from BlockingQueue. If the BlockingQueue did not contain the element obj, then this method returns false.
Note: The remove() method of BlockingQueue has been inherited from the Queue class in Java.
Below programs illustrates remove(Object obj) method of BlockingQueue class:
Program 1:
import
java.util.concurrent.LinkedBlockingQueue;
import
java.util.concurrent.BlockingQueue;
public
class
GFG {
`` public
static
void
main(String[] args)
`` throws
InterruptedException
`` {
`` int
capacityOfQueue =
4
;
`` BlockingQueue<String> BQ
`` =
new
LinkedBlockingQueue<String>(capacityOfQueue);
`` BQ.put(
"Karan"
);
`` BQ.put(
"Suraj"
);
`` BQ.put(
"Harsh"
);
`` BQ.put(
"Rahul"
);
`` System.out.println(
"Items in Queue are "
+ BQ);
`` boolean
try1 = BQ.remove(
"Karan"
);
`` System.out.println(
"String name Karan is removed :"
`` + try1);
`` boolean
try2 = BQ.remove(
"Sunny"
);
`` System.out.println(
"String name Sunny is removed :"
`` + try2);
`` boolean
try3 = BQ.remove(
"Harsh"
);
`` System.out.println(
"String name Harsh is removed :"
`` + try2);
`` System.out.println(
"After Removing Some Elements:"
);
`` System.out.println(
"Items in Queue are "
+ BQ);
`` }
}
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:
import
java.util.Iterator;
import
java.util.concurrent.LinkedBlockingQueue;
import
java.util.concurrent.BlockingQueue;
public
class
GFG {
`` public
void
removeDemo()
throws
InterruptedException
`` {
`` int
capacityOfQueue =
5
;
`` BlockingQueue<Employee> BQ
`` =
new
LinkedBlockingQueue<Employee>(capacityOfQueue);
`` Employee emp1 =
new
Employee(
"Ranjeet"
,
"Tester"
,
"29000"
,
27
);
`` Employee emp2 =
new
Employee(
"Sanjeet"
,
"Manager"
,
"98000"
,
34
);
`` Employee emp3 =
new
Employee(
"Karan"
,
"Analyst"
,
"44000"
,
30
);
`` BQ.put(emp1);
`` BQ.put(emp2);
`` BQ.put(emp3);
`` System.out.println(
"Before removing Elements"
);
`` Iterator itr = BQ.iterator();
`` while
(itr.hasNext())
`` System.out.println(itr.next());
`` BQ.remove(emp2);
`` BQ.remove(emp1);
`` System.out.println(
"After removing Some Elements"
);
`` itr = BQ.iterator();
`` while
(itr.hasNext())
`` System.out.println(itr.next());
`` }
`` public
class
Employee {
`` public
String name;
`` public
String position;
`` public
String salary;
`` public
int
Age;
`` Employee(String name, String position,
`` String salary,
int
age)
`` {
`` this
.name = name;
`` this
.position = position;
`` this
.salary = salary;
`` this
.Age = age;
`` }
`` @Override
`` public
String toString()
`` {
`` return
"Employee [name="
+ name +
", position="
`` + position +
", salary="
+ salary +
", Age="
+ Age +
"]"
;
`` }
`` }
`` public
static
void
main(String[] args)
`` {
`` GFG gfg =
new
GFG();
`` try
{
`` gfg.removeDemo();
`` }
`` catch
(InterruptedException e) {
`` e.printStackTrace();
`` }
`` }
}
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]