The contains(Object obj) method of LinkedBlockingQueue returns true if queue contains the object obj passed as parameter. It returns true, if and only if, this queue contains at least one element e which is equal to object o passed as parameter i.e. e.equals(o). This method is very helpful to check whether the queue has an element or not.
Syntax:
public boolean contains(Object o)
Parameter: This method takes a mandatory parameter o which is the object to be check in the queue.
Return Value: This method returns true if this queue contains the object passed as parameter. Else it returns false.
Below programs illustrate contains() method of LinkedBlockingQueue class:
Program 1: Check whether a queue of numbers contains a number n or not. Number n is passed as input to contains() of LinkedBlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 50 ;
LinkedBlockingQueue<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
linkedQueue.add( 2300 );
linkedQueue.add( 1322 );
linkedQueue.add( 8945 );
linkedQueue.add( 6512 );
System.out.println( "linkedQueue: " + linkedQueue);
boolean response1 = linkedQueue.contains( 6512 );
System.out.println( "LinkedBlockingQueue contains "
+ "number 6512 : "
+ response1);
boolean response2 = linkedQueue.contains( 5324 );
System.out.println( "LinkedBlockingQueue contains"
+ " number 5324 : "
+ response2);
}
}
|
Output:
linkedQueue: [2300, 1322, 8945, 6512]
LinkedBlockingQueue contains number 6512 : true
LinkedBlockingQueue contains number 5324 : false
Program 2: Check whether a queue of Employees contains certain employee e or not. Details of employee e are passed as input to contains() of LinkedBlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this .name = name;
this .position = position;
this .salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", position="
+ position + ", salary=" + salary + "]" ;
}
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.containsMethodExample();
}
public void containsMethodExample()
{
int capacity = 50 ;
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacity);
Employee emp1 = new Employee( "Aman" ,
"Analyst" ,
"24000" );
Employee emp2 = new Employee( "Sachin" ,
"Developer" ,
"39000" );
linkedQueue.add(emp1);
linkedQueue.add(emp2);
System.out.println( "linkedQueue: " + linkedQueue);
Employee checkEmp1 = new Employee( "Sanjeev" ,
"Tester" ,
"26000" );
boolean response1 = linkedQueue.contains(emp1);
System.out.println( "linkedQueue contains "
+ emp1.toString() + " : "
+ response1);
boolean response2 = linkedQueue.contains(checkEmp1);
System.out.println( "linkedQueue contains "
+ checkEmp1.toString() + " : "
+ response2);
}
}
|
Output:
linkedQueue:
[Employee [name=Aman, position=Analyst, salary=24000],
Employee [name=Sachin, position=Developer, salary=39000]]
linkedQueue contains Employee [name=Aman, position=Analyst, salary=24000] : true
linkedQueue contains Employee [name=Sanjeev, position=Tester, salary=26000] : false
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#contains-java.lang.Object-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 May, 2019
Like Article
Save Article