Skip to content
Related Articles

Related Articles

Improve Article

ConcurrentLinkedQueue contains() method in Java

  • Last Updated : 26 Nov, 2018
Geek Week

The contains() method of ConcurrentLinkedQueue returns true if ConcurrentLinkedQueue contains the object o, passed as parameter. This method returns true if and only if this ConcurrentLinkedQueue 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: This method takes a single parameter O which represents object to check whether ConcurrentLinkedQueue contains the specified object.

Returns: This method returns true if this ConcurrentLinkedQueue contains the object.

Below programs illustrate contains() method of ConcurrentLinkedQueue:



Example 1:




// Java Program Demonstrate contains()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String>
            queue = new ConcurrentLinkedQueue<String>();
  
        // Add String to queue
        queue.add("Aman");
        queue.add("Amar");
        queue.add("Sanjeet");
        queue.add("Rabi");
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // check whether queue contains String "Aman"
        boolean response1 = queue.contains("Aman");
  
        // print after applying contains method
        System.out.println("queue contains String \"Aman\" : "
                           + response1);
  
        // check whether queue contains "Ram"
        boolean response2 = queue.contains("Ram");
  
        // print after applying contains method
        System.out.println("queue contains String \"Ram\" : "
                           + response2);
    }
}
Output:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
queue contains String "Aman" : true
queue contains String "Ram" : false

Example 2:




// Java Program Demonstrate contains()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer>
            queue = new ConcurrentLinkedQueue<Integer>();
  
        // Add Numbers to queue
        queue.add(4353);
        queue.add(7824);
        queue.add(78249);
        queue.add(8724);
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // check whether queue contains 78249
        boolean response1 = queue.contains(78249);
  
        // print after applying contains method
        System.out.println("queue contains Number 78249 : "
                           + response1);
  
        // check whether queue contains 9876
        boolean response2 = queue.contains(9876);
  
        // print after applying contains method
        System.out.println("queue contains Number 9876: "
                           + response2);
    }
}
Output:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
queue contains Number 78249 : true
queue contains Number 9876: false

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :