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


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


ArrayBlockingQueue是有界的阻塞队列,该队列内部使用数组支持的元素存储。

  • ArrayBlockingQueue类是Java Collections Framework的成员。
  • 有界意味着它将具有固定的大小,您不能存储数量超过队列容量的元素。
  • 队列还遵循FIFO(先进先出)规则,用于存储和删除队列中的元素。
  • 如果您尝试将一个元素放入一个完整的队列或从一个空队列中取出一个元素,那么该队列将阻塞。

如果队列包含作为参数传递的对象o,则contains(Object o)方法将返回true。我们可以说,当且仅当此队列包含至少一个等于作为参数传递的对象o的元素e,即o.equals(e)时,该方法才返回true。
用法:

public boolean contains(Object o)

参数:
o –检查队列是否包含指定对象的对象。
返回值:
如果此队列包含对象,则为true


以下示例程序旨在说明ArrayBlockingQueue的包含方法。
例子1

// Java Program Demonstrate contains(Object o) 
// method of ArrayBlockingQueue. 
  
import java.util.concurrent.ArrayBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of ArrayBlockingQueue 
        int capacity = 5; 
  
        // create object of ArrayBlockingQueue 
        ArrayBlockingQueue<Integer> queue 
            = new ArrayBlockingQueue<Integer>(capacity); 
  
        // Add elements to ArrayBlockingQueue 
        queue.add(23); 
        queue.add(32); 
        queue.add(45); 
        queue.add(12); 
  
        // check whether queue contains 23 
        boolean response1 = queue.contains(23); 
  
        // print after applying contains method with 23 as parameter 
        System.out.println("queue contains 23 : " + response1); 
  
        // check whether queue contains 99 
        boolean response2 = queue.contains(99); 
  
        // print after applying contains method with 99 as parameter 
        System.out.println("queue contains 99 : " + response2); 
    } 
}
输出:
queue contains 23 : true
queue contains 99 : false

例子2

// Java Program Demonstrate contains(Object o) 
// method of ArrayBlockingQueue. 
import java.util.concurrent.ArrayBlockingQueue; 
  
public class GFG { 
  
    // create a User Object with name and age as attribute 
    public class User { 
  
        public String name; 
        public String age; 
        User(String name, String age) 
        { 
            this.name = name; 
            this.age = age; 
        } 
    } 
  
    // Main Method 
    public static void main(String[] args) 
    { 
        GFG gfg = new GFG(); 
        gfg.containsMethodExample(); 
    } 
  
    // Method to give example of contains function 
    public void containsMethodExample() 
    { 
  
        // define capacity of ArrayBlockingQueue 
        int capacity = 5; 
  
        // create object of ArrayBlockingQueue 
        ArrayBlockingQueue<User> queue 
            = new ArrayBlockingQueue<User>(capacity); 
  
        User user1 = new User("Aman", "24"); 
        User user2 = new User("Amar", "23"); 
        User user3 = new User("Sanjeet", "25"); 
        User user4 = new User("Suvo", "26"); 
  
        // Add Objects to ArrayBlockingQueue 
        queue.add(user1); 
        queue.add(user2); 
        queue.add(user3); 
        queue.add(user4); 
  
        User user5 = new User("Ravi", "22"); 
        // check whether queue contains User1 
        boolean response1 = queue.contains(user1); 
  
        // print after applying contains method with user1 as parameter 
        System.out.println("queue contains User having name "
                           + user1.name + " : " + response1); 
  
        // check whether queue contains User5 
        boolean response2 = queue.contains(user5); 
  
        // print after applying contains method with user1 as parameter 
        System.out.println("queue contains User having name "
                           + user5.name + " : " + response2); 
    } 
}
输出:
queue contains User having name Aman : true
queue contains User having name Ravi : false

参考:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#contains(java.lang.Object)



相关用法


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