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


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


如果ConcurrentLinkedQueue包含作為參數傳遞的對象o,則ConcurrentLinkedQueue的contains()方法返回true。當且僅當此ConcurrentLinkedQueue包含至少一個等於作為參數傳遞的對象o的元素e,即o.equals(e)時,此方法才返回true。

用法:

public boolean contains(Object o)

參數:此方法采用代表對象的單個參數O來檢查ConcurrentLinkedQueue是否包含指定的對象。


返回值:如果此ConcurrentLinkedQueue包含對象,則此方法返回true。

以下示例程序旨在說明ConcurrentLinkedQueue的contains()方法:

示例1:

// Java Program Demonstrate contains() 
// method of ConcurrentLinkedQueue 
  
import java.util.concurrent.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ConcurrentLinkedQueue 
        ConcurrentLinkedQueue<String> 
            queue = new ConcurrentLinkedQueue<String>(); 
  
        // Add String to queue 
        queue.add("Aman"); 
        queue.add("Amar"); 
        queue.add("Sanjeet"); 
        queue.add("Rabi"); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
  
        // check whether queue contains String "Aman" 
        boolean response1 = queue.contains("Aman"); 
  
        // print after applying contains method 
        System.out.println("queue contains String \"Aman\" : "
                           + response1); 
  
        // check whether queue contains "Ram" 
        boolean response2 = queue.contains("Ram"); 
  
        // print after applying contains method 
        System.out.println("queue contains String \"Ram\" : "
                           + response2); 
    } 
}
輸出:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
queue contains String "Aman" : true
queue contains String "Ram" : false

示例2:

// Java Program Demonstrate contains() 
// method of ConcurrentLinkedQueue 
  
import java.util.concurrent.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ConcurrentLinkedQueue 
        ConcurrentLinkedQueue<Integer> 
            queue = new ConcurrentLinkedQueue<Integer>(); 
  
        // Add Numbers to queue 
        queue.add(4353); 
        queue.add(7824); 
        queue.add(78249); 
        queue.add(8724); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
  
        // check whether queue contains 78249 
        boolean response1 = queue.contains(78249); 
  
        // print after applying contains method 
        System.out.println("queue contains Number 78249 : "
                           + response1); 
  
        // check whether queue contains 9876 
        boolean response2 = queue.contains(9876); 
  
        // print after applying contains method 
        System.out.println("queue contains Number 9876: "
                           + response2); 
    } 
}
輸出:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
queue contains Number 78249 : true
queue contains Number 9876: false

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



相關用法


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