Open In App

ArrayBlockingQueue contains() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.

  • ArrayBlockingQueue class is a member of the Java Collections Framework.
  • Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
  • The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
  • If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.

The contains(Object o) method returns true if queue contains the object o passed as parameter. We can say that 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. o.equals(e).
Syntax:

public boolean contains(Object o)

Parameter:
o – object to check whether queue contains the specified object.
Return Value:
true if this queue contains the object

Below program illustrate contains method of ArrayBlockingQueue.
Example 1




// Java Program Demonstrate contains(Object o)
// method of ArrayBlockingQueue.
  
import java.util.concurrent.ArrayBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 5;
  
        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> queue
            = new ArrayBlockingQueue<Integer>(capacity);
  
        // Add elements to ArrayBlockingQueue
        queue.add(23);
        queue.add(32);
        queue.add(45);
        queue.add(12);
  
        // check whether queue contains 23
        boolean response1 = queue.contains(23);
  
        // print after applying contains method with 23 as parameter
        System.out.println("queue contains 23 : " + response1);
  
        // check whether queue contains 99
        boolean response2 = queue.contains(99);
  
        // print after applying contains method with 99 as parameter
        System.out.println("queue contains 99 : " + response2);
    }
}


Output:

queue contains 23 : true
queue contains 99 : false

Example 2




// Java Program Demonstrate contains(Object o)
// method of ArrayBlockingQueue.
import java.util.concurrent.ArrayBlockingQueue;
  
public class GFG {
  
    // create a User Object with name and age as attribute
    public class User {
  
        public String name;
        public String age;
        User(String name, String age)
        {
            this.name = name;
            this.age = age;
        }
    }
  
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.containsMethodExample();
    }
  
    // Method to give example of contains function
    public void containsMethodExample()
    {
  
        // define capacity of ArrayBlockingQueue
        int capacity = 5;
  
        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<User> queue
            = new ArrayBlockingQueue<User>(capacity);
  
        User user1 = new User("Aman", "24");
        User user2 = new User("Amar", "23");
        User user3 = new User("Sanjeet", "25");
        User user4 = new User("Suvo", "26");
  
        // Add Objects to ArrayBlockingQueue
        queue.add(user1);
        queue.add(user2);
        queue.add(user3);
        queue.add(user4);
  
        User user5 = new User("Ravi", "22");
        // check whether queue contains User1
        boolean response1 = queue.contains(user1);
  
        // print after applying contains method with user1 as parameter
        System.out.println("queue contains User having name "
                           + user1.name + " : " + response1);
  
        // check whether queue contains User5
        boolean response2 = queue.contains(user5);
  
        // print after applying contains method with user1 as parameter
        System.out.println("queue contains User having name "
                           + user5.name + " : " + response2);
    }
}


Output:

queue contains User having name Aman : true
queue contains User having name Ravi : false

Reference:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#contains(java.lang.Object)



Last Updated : 26 Nov, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads