Home Java LinkedList Methods contains() Java LinkedList contains() Method Definition In Java, contains(o) returns true if the LinkedList has at least one element equal to o, and false otherwise. The list is not changed.
Elements are compared with equals, and the search is linear: Java checks the elements one by one from the front. A null argument is allowed and matches a null element.
An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.
Syntax Pass the value to look for.
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
boolean a = list.contains("B"); // true
boolean b = list.contains("Z"); // false Call What it does list.contains(o)true if an equal element exists.list.indexOf(o)Also gives the position, or -1. list.containsAll(c)Checks a whole collection at once. set.contains(o)The same question asked of a HashSet, which answers much faster.
contains only reads the list, so it never throws an exception for a normal value.
Library declarations // java.util.LinkedList
public boolean contains(Object o) Parameters Parameter Description o (Object)The element to look for. May be null.
Return Value Return Value Description booleantrue if the list contains at least one element equal to o.
Important
The argument type is Object, so the compiler will not warn you about a wrong type. On a LinkedList<Integer>, contains(5L) is false, because a Long is never equal to an Integer. If you check membership often on a big collection, use a HashSet, since a list must scan all its elements.
Compare with Similar Methods Ways to test for an element:
Java Examples 1. Yes or no 2. Null elements 3. Integer versus Long Example 1 — Check Whether a Value Is in the List Ask a list of allowed users whether several names are on it.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListContainsDemo {
public static void main(String[] args) {
LinkedList<String> allowed = new LinkedList<>(
Arrays.asList("root", "alice", "bob"));
System.out.println("alice: " + allowed.contains("alice"));
System.out.println("carol: " + allowed.contains("carol"));
System.out.println("Alice: " + allowed.contains("Alice"));
String user = "bob";
if (allowed.contains(user)) {
System.out.println("welcome back, " + user);
}
}
} alice: true
carol: false
Alice: false
welcome back, bob How It Works Three allowed users. The list holds root, alice and bob.A present value. "alice" is in the list, so the answer is true.Missing and misspelled. "carol" is not there, and "Alice" with a capital A is a different string, so both give false.Used in an if. Because "bob" is in the list, the welcome message is printed.Example 2 — Search for null in a List Check whether a list holds a null element, before and after adding one.
import java.util.LinkedList;
public class LinkedListContainsNull {
public static void main(String[] args) {
LinkedList<String> answers = new LinkedList<>();
answers.add("yes");
answers.add("no");
System.out.println("has null: " + answers.contains(null));
answers.add(null);
System.out.println("has null: " + answers.contains(null));
System.out.println("size: " + answers.size());
}
} has null: false
has null: true
size: 3 How It Works Two real answers. No element is null yet.First check. contains(null) is allowed, and returns false.A null is stored. A LinkedList accepts null elements, so add(null) works.Second check. Now contains(null) returns true, and the size is 3.Example 3 — Avoid the Integer and Long Trap Check a list of Integer values with an int and with a long, and see why the results differ.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListContainsTrap {
public static void main(String[] args) {
LinkedList<Integer> ids = new LinkedList<>(Arrays.asList(5, 10, 15));
System.out.println("contains(5): " + ids.contains(5));
System.out.println("contains(5L): " + ids.contains(5L));
System.out.println("5L == 5: " + (5L == 5));
System.out.println("fixed: " + ids.contains((int) 5L));
}
} contains(5): true
contains(5L): false
5L == 5: true
fixed: true How It Works A list of Integer objects. The list holds 5, 10 and 15.An int matches. 5 is boxed to an Integer, which equals the stored 5.A long does not. 5L is boxed to a Long. A Long is never equal to an Integer, so the answer is false.The plain comparison is true. Primitive 5L == 5 compares numbers. Casting the long back with (int) makes contains find the value again.Frequently Asked Questions How do I check if a LinkedList contains a value in Java? Call list.contains(value). It returns true if an equal element is in the list.
How does LinkedList contains() compare elements? It uses equals. For your own classes, override equals to make contains work as you expect.
Can contains() search for null? Yes. contains(null) returns true if the list holds a null element.
Why does contains() return false for a number that looks present? A common cause is a type mismatch, such as searching for a Long in a list of Integer values. The two types are never equal.
Is contains() fast? It scans the elements one by one, so the time grows with the size of the list. Use a HashSet for fast membership tests.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion