📜  Java中的 LinkedBlockingDeque contains() 方法

📅  最后修改于: 2022-05-13 01:54:51.640000             🧑  作者: Mango

Java中的 LinkedBlockingDeque contains() 方法

LinkedBlockingDequecontains(Object o)方法检查参数中传递的元素是否存在于容器中。如果元素存在于容器中,则返回 true,否则返回 false 值。

句法:

public boolean contains(Object o)

参数:此方法接受一个强制参数o ,它在容器中的存在将在容器中检查。

返回:如果元素存在,此方法返回 true,否则返回 false。

下面的程序说明了 LinkedBlockingDeque 的 contains() 方法:

方案一:

// Java Program Demonstrate contains()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(10);
        LBD.add(20);
        LBD.add(30);
        LBD.add(40);
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        // check for presence using function
        if (LBD.contains(10)) {
            System.out.println("Linked Blocking Deque contains 10");
        }
        else {
            System.out.println("Linked Blocking Deque does not contain 10");
        }
    }
}
输出:
Linked Blocking Deque: [10, 20, 30, 40]
Linked Blocking Deque contains 10

方案二:

// Java Program Demonstrate contains()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add("ab");
        LBD.add("cd");
        LBD.add("fg");
        LBD.add("xz");
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        // check for presence using function
        if (LBD.contains("go")) {
            System.out.println("Linked Blocking Deque contains 'go'");
        }
        else {
            System.out.println("Linked Blocking Deque does not contain 'go'");
        }
    }
}
输出:
Linked Blocking Deque: [ab, cd, fg, xz]
Linked Blocking Deque does not contain 'go'

参考: https: Java Java.lang.Object)