Last Updated : 14 Oct, 2019
The
contains(Object o)method of
BlockingQueueinterface checks if the passed element in the parameter exists in the container or not. It returns true if the element exists in the container else it returns a false value.
Syntax:public boolean contains(Object o)Parameters:
This method accepts a mandatory parameter
owhose presence in the container is to be checked in the container.
Returns:This method returns true if the element is present, else it returns false.
Note: The
contains()method of
BlockingQueuehas been inherited from the
Queueclass in Java. Below programs illustrate contains() method of BlockingQueue:
Program 1: Java
// Java Program Demonstrate contains()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingQueue
BlockingQueue<Integer> BQ
= new LinkedBlockingQueue<Integer>();
// Add numbers to end of BlockingQueue
BQ.add(10);
BQ.add(20);
BQ.add(30);
BQ.add(40);
// before removing print queue
System.out.println("Blocking Queue: " + BQ);
// check for presence using function
if (BQ.contains(10)) {
System.out.println("Blocking Queue contains 10");
}
else {
System.out.println("Blocking Queue does not contain 10");
}
}
}
Output:
Blocking Queue: [10, 20, 30, 40] Blocking Queue contains 10Program 2: Java
// Java Program Demonstrate contains()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>();
// Add numbers to end of BlockingQueue
BQ.add("ab");
BQ.add("cd");
BQ.add("fg");
BQ.add("xz");
// before removing print queue
System.out.println("Blocking Queue: " + BQ);
// check for presence using function
if (BQ.contains("go")) {
System.out.println("Blocking Queue contains 'go'");
}
else {
System.out.println("Blocking Queue does not contain 'go'");
}
}
}
Output:
Blocking Queue: [ab, cd, fg, xz] Blocking Queue does not contain 'go'Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html#contains(java.lang.Object)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4