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


Java LinkedBlockingQueue contains()用法及代碼示例


如果隊列包含作為參數傳遞的對象obj,則LinkedBlockingQueue的contains(Object obj)方法將返回true。當且僅當此隊列包含至少一個元素e等於等於作為參數傳遞的對象o即e.equals(o)時,它返回true。此方法對於檢查隊列中是否包含元素非常有幫助。

用法:

public boolean contains(Object o)

參數:此方法采用強製參數o,該參數是要在隊列中檢查的對象。


返回值:如果此隊列包含作為參數傳遞的對象,則此方法返回true。否則返回false。

下麵的程序說明LinkedBlockingQueue類的contains()方法:

示例1:檢查數字隊列中是否包含數字n。數字n作為輸入傳遞給LinkedBlockingQueue的contains()。

// Java Program Demonstrate contains(Object o) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 50; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<Integer> linkedQueue 
            = new LinkedBlockingQueue<Integer>(capacityOfQueue); 
  
        // Add element to LinkedBlockingQueue 
        linkedQueue.add(2300); 
        linkedQueue.add(1322); 
        linkedQueue.add(8945); 
        linkedQueue.add(6512); 
  
        // print LinkedQueue 
        System.out.println("linkedQueue: " + linkedQueue); 
  
        // check whether LinkedBlockingQueue contains 6512 
        boolean response1 = linkedQueue.contains(6512); 
  
        // print result 
        System.out.println("LinkedBlockingQueue contains "
                           + "number 6512 : "
                           + response1); 
  
        // check whether LinkedBlockingQueue contains 5324 
        boolean response2 = linkedQueue.contains(5324); 
  
        // print result 
        System.out.println("LinkedBlockingQueue contains"
                           + " number 5324 : "
                           + response2); 
    } 
}
輸出:
linkedQueue: [2300, 1322, 8945, 6512]

LinkedBlockingQueue contains number 6512 : true
LinkedBlockingQueue contains number 5324 : false

示例2:檢查員工隊列中是否包含某些員工e。將雇員e的詳細信息作為輸入傳遞給LinkedBlockingQueue的contains()。

// Java Program Demonstrate contains(Object o) 
// method of LinkedBlockingQueue. 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    // create an Employee Object with 
    // position and salary as an attribute 
    public class Employee { 
  
        public String name; 
        public String position; 
        public String salary; 
  
        Employee(String name, String position, String salary) 
        { 
            this.name = name; 
            this.position = position; 
            this.salary = salary; 
        } 
  
        @Override
        public String toString() 
        { 
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary + "]"; 
        } 
    } 
  
    // Main Method 
    public static void main(String[] args) 
    { 
        GFG gfg = new GFG(); 
        gfg.containsMethodExample(); 
    } 
  
    public void containsMethodExample() 
    { 
  
        // define capacity of LinkedBlockingQueue 
        int capacity = 50; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<Employee> linkedQueue 
            = new LinkedBlockingQueue<Employee>(capacity); 
  
        Employee emp1 = new Employee("Aman", 
                                     "Analyst", 
                                     "24000"); 
        Employee emp2 = new Employee("Sachin", 
                                     "Developer", 
                                     "39000"); 
  
        // Add Employee Objects to linkedQueue 
        linkedQueue.add(emp1); 
        linkedQueue.add(emp2); 
  
        // print LinkedQueue 
        System.out.println("linkedQueue: " + linkedQueue); 
  
        // Employee to be checked 
        Employee checkEmp1 = new Employee("Sanjeev", 
                                          "Tester", 
                                          "26000"); 
  
        // check whether linkedQueue contains emp1 
        boolean response1 = linkedQueue.contains(emp1); 
  
        // print results 
        System.out.println("linkedQueue contains "
                           + emp1.toString() + " : "
                           + response1); 
  
        // check whether linkedQueue contains checkEmp1 
        boolean response2 = linkedQueue.contains(checkEmp1); 
  
        // print results 
        System.out.println("linkedQueue contains "
                           + checkEmp1.toString() + " : "
                           + response2); 
    } 
}
輸出:
linkedQueue: 
[Employee [name=Aman, position=Analyst, salary=24000], 
Employee [name=Sachin, position=Developer, salary=39000]]

linkedQueue contains Employee [name=Aman, position=Analyst, salary=24000] : true
linkedQueue contains Employee [name=Sanjeev, position=Tester, salary=26000] : false

參考:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#contains-java.lang.Object-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LinkedBlockingQueue contains() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。