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); } } |
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); } } |
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] queue contains Number 78249 : true queue contains Number 9876: false