📌  相关文章
📜  Java中的 PriorityBlockingQueue contains() 方法

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

Java中的 PriorityBlockingQueue contains() 方法

contains(Object o)方法检查PriorityBlockingQueue是否包含对象 o。此方法返回 true,当且仅当此队列包含至少一个元素 e,该元素等于作为参数 ieeequals(o) 传递的对象 o。如果 queue 不包含 Object o,则方法返回 false。

句法:

public boolean contains(Object o)

参数:该方法需要一个强制参数o ,它是PriorityBlockingQueue中要检查的对象。

返回:如果此队列包含作为参数传递的对象,则此方法返回true 。否则返回false

异常:此方法不会抛出任何异常。

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

示例 1:

// Java Program Demonstrate contains(Object o)
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue PrioQueue
            = new PriorityBlockingQueue(capacityOfQueue);
  
        // Add element to PriorityBlockingQueue
        PrioQueue.add(4641515);
        PrioQueue.add(46514561);
        PrioQueue.add(56156);
        PrioQueue.add(948964165);
  
        // print PrioQueue
        System.out.println("PrioQueue: " + PrioQueue);
  
        // check whether PriorityBlockingQueue contains 56156
        boolean answer1 = PrioQueue.contains(56156);
  
        // print result
        System.out.println("PriorityBlockingQueue contains "
                           + "number 56156 : "
                           + answer1);
  
        // check whether PriorityBlockingQueue contains 46545
        boolean answer2 = PrioQueue.contains(46545);
  
        // print result
        System.out.println("PriorityBlockingQueue contains"
                           + " number 46545 : "
                           + answer2);
    }
}
输出:
PrioQueue: [56156, 46514561, 4641515, 948964165]
PriorityBlockingQueue contains number 56156 : true
PriorityBlockingQueue contains number 46545 : false

示例 2:演示包含名称列表的 PriorityBlockingQueue 的 contains() 方法。

// Java Program Demonstrate contains(Object o)
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 10;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue names
            = new PriorityBlockingQueue(capacityOfQueue);
  
        // Add names
        names.add("Geeks");
        names.add("forGeeks");
        names.add("A");
        names.add("Computer");
        names.add("Portal");
  
        // print queue details
        System.out.println("List of Names: " + names);
  
        // check whether PriorityBlockingQueue contains Geeks
        boolean answer1 = names.contains("Geeks");
  
        // print result
        System.out.println("Does names contains "
                           + "Geeks: "
                           + answer1);
  
        // check whether PriorityBlockingQueue contains SandeepJain
        boolean answer2 = names.contains("SandeepJain");
  
        // print result
        System.out.println("Does names contains "
                           + "SandeepJain: "
                           + answer2);
    }
}
输出:
List of Names: [A, Computer, Geeks, forGeeks, Portal]
Does names contains Geeks: true
Does names contains SandeepJain: false

参考:
https://docs.oracle.com/javase/8/docs/api/java Java