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.
// Java Program Demonstrate contains(Object o) // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of LinkedBlockingQueue int capacityOfQueue = 50; // create object of LinkedBlockingQueue LinkedBlockingQueue<Integer> linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue); // Add element to LinkedBlockingQueue linkedQueue.add(2300); linkedQueue.add(1322); linkedQueue.add(8945); linkedQueue.add(6512); // print LinkedQueue System.out.println("linkedQueue: " + linkedQueue); // check whether LinkedBlockingQueue contains 6512 boolean response1 = linkedQueue.contains(6512); // print result System.out.println("LinkedBlockingQueue contains " + "number 6512 : " + response1); // check whether LinkedBlockingQueue contains 5324 boolean response2 = linkedQueue.contains(5324); // print result System.out.println("LinkedBlockingQueue contains" + " number 5324 : " + response2); } } |
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.
// Java Program Demonstrate contains(Object o) // method of LinkedBlockingQueue. import java.util.concurrent.LinkedBlockingQueue; public class GFG { // create an Employee Object with // position and salary as an attribute 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 + "]"; } } // Main Method public static void main(String[] args) { GFG gfg = new GFG(); gfg.containsMethodExample(); } public void containsMethodExample() { // define capacity of LinkedBlockingQueue int capacity = 50; // create object of LinkedBlockingQueue LinkedBlockingQueue<Employee> linkedQueue = new LinkedBlockingQueue<Employee>(capacity); Employee emp1 = new Employee("Aman", "Analyst", "24000"); Employee emp2 = new Employee("Sachin", "Developer", "39000"); // Add Employee Objects to linkedQueue linkedQueue.add(emp1); linkedQueue.add(emp2); // print LinkedQueue System.out.println("linkedQueue: " + linkedQueue); // Employee to be checked Employee checkEmp1 = new Employee("Sanjeev", "Tester", "26000"); // check whether linkedQueue contains emp1 boolean response1 = linkedQueue.contains(emp1); // print results System.out.println("linkedQueue contains " + emp1.toString() + " : " + response1); // check whether linkedQueue contains checkEmp1 boolean response2 = linkedQueue.contains(checkEmp1); // print results System.out.println("linkedQueue contains " + checkEmp1.toString() + " : " + response2); } } |
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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- LinkedBlockingQueue put() method in Java with Examples
- LinkedBlockingQueue iterator() method in Java
- LinkedBlockingQueue clear() method in Java
- LinkedBlockingQueue peek() method in Java
- LinkedBlockingQueue remainingCapacity() method in Java
- LinkedBlockingQueue remove() method in Java
- LinkedBlockingQueue size() method in Java
- LinkedBlockingQueue take() Method in Java with Examples
- LinkedBlockingQueue toString() Method in Java with Examples
- Java 8 | LinkedBlockingQueue spliterator() method with Examples
- LinkedBlockingQueue poll() method in Java
- LinkedBlockingQueue | offer() Method in JAVA
- LinkedBlockingQueue drainTo() method in Java
- LinkedBlockingQueue toArray() method in Java
- LinkedBlockingQueue toString() method in Java
- LinkedBlockingQueue Class in Java
- Difference Between ArrayBlockingQueue and LinkedBlockingQueue
- LinkedBlockingDeque contains() method in Java
- HashSet contains() Method in Java
- Java String contains() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2

