ConcurrentLinkedQueue contains() method in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
queue contains String "Aman" : true
queue contains String "Ram" : false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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-



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.