当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java PriorityBlockingQueue contains()用法及代码示例


contains(Object o)方法检查PriorityBlockingQueue是否包含对象o。当且仅当此队列包含至少一个等于作为参数传递的对象o的元素e,即equals(o)时,此方法返回true。如果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<Integer> PrioQueue 
            = new PriorityBlockingQueue<Integer>(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<String> names 
            = new PriorityBlockingQueue<String>(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/util/concurrent/PriorityBlockingQueue.html#contains-java.lang.Object-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue contains() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。