當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。