📜  Java中的 ConcurrentLinkedQueue contains() 方法

📅  最后修改于: 2022-05-13 01:54:27.680000             🧑  作者: Mango

Java中的 ConcurrentLinkedQueue contains() 方法

如果 ConcurrentLinkedQueue 包含作为参数传递的对象 o,则ConcurrentLinkedQueuecontains()方法返回 true。当且仅当此 ConcurrentLinkedQueue 包含至少一个等于作为参数 ieoequals(e) 传递的对象 o 的元素 e 时,此方法才返回 true。

句法:

public boolean contains(Object o)

参数:该方法接受一个代表对象的参数O来检查ConcurrentLinkedQueue是否包含指定的对象。

返回:如果此 ConcurrentLinkedQueue 包含对象,则此方法返回true

下面的程序说明了 ConcurrentLinkedQueue 的 contains() 方法:

示例 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
            queue = new ConcurrentLinkedQueue();
  
        // 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

示例 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
            queue = new ConcurrentLinkedQueue();
  
        // 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

参考: https: Java/util/concurrent/ConcurrentLinkedQueue.html#contains-java.lang.Object-