PriorityBlockingQueue contains() method in Java

The contains(Object o) method checks whether PriorityBlockingQueue contains an object o or not. This method 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). If queue does not contains the Object o, then method returns false.

Syntax:

public boolean contains(Object o)

Parameter: This method takes a mandatory parameter o which is the object to be checked in the PriorityBlockingQueue.



Returns: This method returns true if this queue contains the object passed as parameter. Else it returns false.

Exception: This method does not throw any Exception.

Below program illustrate contains() method of PriorityBlockingQueue:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate contains(Object o)
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> PrioQueue
            = new PriorityBlockingQueue<Integer>(capacityOfQueue);
  
        // Add element to PriorityBlockingQueue
        PrioQueue.add(4641515);
        PrioQueue.add(46514561);
        PrioQueue.add(56156);
        PrioQueue.add(948964165);
  
        // print PrioQueue
        System.out.println("PrioQueue: " + PrioQueue);
  
        // check whether PriorityBlockingQueue contains 56156
        boolean answer1 = PrioQueue.contains(56156);
  
        // print result
        System.out.println("PriorityBlockingQueue contains "
                           + "number 56156 : "
                           + answer1);
  
        // check whether PriorityBlockingQueue contains 46545
        boolean answer2 = PrioQueue.contains(46545);
  
        // print result
        System.out.println("PriorityBlockingQueue contains"
                           + " number 46545 : "
                           + answer2);
    }
}

chevron_right


Output:

PrioQueue: [56156, 46514561, 4641515, 948964165]
PriorityBlockingQueue contains number 56156 : true
PriorityBlockingQueue contains number 46545 : false

Example 2: To demonstrate contains() method of PriorityBlockingQueue which contains list of names.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate contains(Object o)
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 10;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<String> names
            = new PriorityBlockingQueue<String>(capacityOfQueue);
  
        // Add names
        names.add("Geeks");
        names.add("forGeeks");
        names.add("A");
        names.add("Computer");
        names.add("Portal");
  
        // print queue details
        System.out.println("List of Names: " + names);
  
        // check whether PriorityBlockingQueue contains Geeks
        boolean answer1 = names.contains("Geeks");
  
        // print result
        System.out.println("Does names contains "
                           + "Geeks: "
                           + answer1);
  
        // check whether PriorityBlockingQueue contains SandeepJain
        boolean answer2 = names.contains("SandeepJain");
  
        // print result
        System.out.println("Does names contains "
                           + "SandeepJain: "
                           + answer2);
    }
}

chevron_right


Output:

List of Names: [A, Computer, Geeks, forGeeks, Portal]
Does names contains Geeks: true
Does names contains SandeepJain: false

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.