In Java, contains(o) returns true if the ArrayDeque holds at least one element equal to o, and false otherwise. Equality is checked with equals, not ==, so two different String objects with the same text match.
The search walks the deque from head to tail, so it takes time proportional to the number of elements. contains(null) simply returns false, because an ArrayDeque never contains null.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.
Syntax
Pass the value you are looking for.
Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("apple", "banana"));
boolean has = deque.contains("banana"); // true
boolean no = deque.contains("grape"); // false
Call
What it does
deque.contains(o)
true if some element equals(o).
deque.contains(null)
Always false; no exception.
!deque.contains(o)
Common guard before adding, to avoid duplicates.
deque.containsAll(c)
true if every element of c is present (inherited from AbstractCollection).
The parameter type is Object, so the compiler accepts any value, even one of the wrong type. A Long never equals an Integer, so searching a Deque<Integer> for 200L returns false.
Library declarations
Java
// java.util.ArrayDeque<E>
public boolean contains(Object o)
// Declared in Collection<E> and Deque<E>
Parameters
Parameter
Description
o
Required. The value to look for. Compared with o.equals(element). May be null (the answer is then false).
Return Value
Return Value
Description
boolean
true if a matching element exists; otherwise false.
Lets you search with your own rule instead of equals.
HashSet.contains(o)
Constant time on average, but a set keeps no order and no duplicates.
Java Examples
Example 1 — Check Whether a Value Is Present
Search a deque of fruit names. Note that the check is case-sensitive.
Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeContainsDemo {
public static void main(String[] args) {
Deque<String> fruits = new ArrayDeque<>(Arrays.asList("apple", "banana", "cherry"));
System.out.println(fruits.contains("banana"));
System.out.println(fruits.contains("Banana"));
System.out.println(fruits.contains("grape"));
}
}
Output
true
false
false
How It Works
Exact match."banana" is in the deque, so the first call returns true.
Case matters.String.equals is case-sensitive, so "Banana" with a capital B is not found.
Missing value."grape" was never added, so the answer is false.
Read-only.contains never changes the deque; it only walks it from head to tail.
Example 2 — equals, null, and Mismatched Types
A separately built String is found, null is not, and a Long does not match an Integer.
Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeContainsEquals {
public static void main(String[] args) {
Deque<String> words = new ArrayDeque<>();
words.add("java");
String built = new String("java");
System.out.println(words.contains(built));
System.out.println(words.contains(null));
Deque<Integer> nums = new ArrayDeque<>(Arrays.asList(100, 200));
System.out.println(nums.contains(200));
System.out.println(nums.contains(200L));
}
}
Output
true
false
true
false
How It Works
Different object, same text.new String("java") is a separate object, but equals compares characters, so it is found.
null is never inside.contains(null) returns false without throwing.
Boxed int.contains(200) boxes 200 to an Integer, which equals the stored Integer 200.
Wrong wrapper.200L is boxed to a Long. Long.equals returns false for an Integer, even with the same number.
Example 3 — Keep Only the First Visit to Each Page
Use contains as a guard so each page name is added only once, in first-visit order.
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeUniqueVisits {
public static void main(String[] args) {
String[] clicks = {"home", "about", "home", "blog", "about"};
Deque<String> history = new ArrayDeque<>();
for (String page : clicks) {
if (!history.contains(page)) {
history.addLast(page);
}
}
System.out.println(history);
System.out.println("size " + history.size());
}
}
Output
[home, about, blog]
size 3
How It Works
Five clicks. Two pages, home and about, are clicked twice.
Guard before adding.!history.contains(page) is true only the first time each page appears.
Repeats skipped. The second home and second about are already present, so they are not added.
Order of first visits. The deque holds three pages in the order they were first seen. For big inputs a LinkedHashSet does this faster.
Frequently Asked Questions
It returns true if the deque holds an element equal to the argument (using equals), otherwise false.
equals. Two different objects with equal content match.
false. ArrayDeque never stores null, and the call does not throw.
It is a linear search, so the time grows with the number of elements.
200L is a Long, and Long.equals never returns true for an Integer, even when the numbers are the same.