Java ArrayBlockingQueue contains() Method

Published by user on

The contains() method in ArrayBlockingQueue checks if the queue has the specified element. If the element is available in the queue, then it returns true. Otherwise, it returns false.

Syntax:

boolean contains(E e)

Parameter – e is the element to check.

Program to Demonstrate ArrayBlockingQueue contains() Method

Let’s have a look at the program.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ArrayBlockingQueueContainsExample {

        public static void main(String[] args) {

                int capacity = 5;
                BlockingQueue salQue = new ArrayBlockingQueue<>(capacity);
                salQue.add(9000);
                salQue.add(18000);
                salQue.add(15000);
                salQue.add(17000);
                salQue.add(13500);

                System.out.println("Queue elements: \n" + salQue);

                System.out.println("Specified element is available? : " + salQue.contains(17000));
                // returns true if the element is available in queue

                System.out.println("Specified element is available? : " + salQue.contains(9500));
                // returns false if the element is not available in queue
        }
}

Output:

Queue elements: 
[9000, 18000, 15000, 17000, 13500]
Specified element is available? : true
Specified element is available? : false

As shown in the above example, the queue contains 17000. Hence it returned true. The queue doesn’t have 9500. Hence it returned false.

Example 2:

Let’s have a look at the program with Employee objects.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ContainsDemo {

        public static void main(String[] args) {

                int capacity = 5;
                BlockingQueue employees = new ArrayBlockingQueue<>(capacity);
                Employee e1 = new Employee(101, "Adam");
                Employee e2 = new Employee(102, "John");
                Employee e3 = new Employee(103, "Bob");
                Employee e4 = new Employee(104, "Mathew");
                employees.add(e1);
                employees.add(e2);
                employees.add(e3);

                for (Employee employee : employees) {
                        System.out.println(employee);
                }

                System.out.println("Specified element is available? : " + employees.contains(e1));
                // returns true if the element is available in queue

                System.out.println("Specified element is available? : " + employees.contains(e4));
                // returns false if the element is available in queue
        }
}

Output:

Employee [employeeId=101, employeeName=Adam]
Employee [employeeId=102, employeeName=John]
Employee [employeeId=103, employeeName=Bob]
Specified element is available? : true
Specified element is available? : false

As shown in the above example, the queue has an Employee e1 object. Hence it returned true.
The queue doesn’t have Employee e4 object. Hence it returned false.

That’s all about the contains() method of ArrayBlockingQueue. Please share if you find this article helpful.

Categories: Java