This example shows how to check if the element exists in the LinkedList in Java. This example also shows how to check if LinkedList contains an object of a custom class.
How to check if the element exists in the LinkedList in Java?
The contains
method of the LinkedList class can be used to check if the element exists in the linked list.
1 |
public boolean contains(Object o) |
The contains
method returns true if the specified object exists in the linked list, false otherwise. In other words, if the linked list contains the element, it returns true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedList; public class LinkedListContainsExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * To check if the LinkedList contains an element, use * the contains method */ //this will return true as the linked list contains "Green" System.out.println( linkedListColors.contains("Green") ); //this will return false, as the linked list does not contain "Yellow" System.out.println( linkedListColors.contains("Yellow") ); } } |
Output
1 2 |
true false |
How to check if the LinkedList contains an object of a custom class?
The contains
method of the LinkedList class returns true if and only if the list contains an element that is equal to the specified element. This equality check is done using the equals
method. The above example worked as expected because the Java String class has implemented the equals
method.
If the linked list you are using has objects of a custom class then the custom class must implement the equals
and hashCode
methods for the contains
method to work properly. Let’s see an example of that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.LinkedList; class Employee{ private Integer id; private String name; public Employee(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + " => " + this.name + "]"; } } public class LinkedListContainsExample { public static void main(String[] args) { LinkedList<Employee> linkedListEmployees = new LinkedList<Employee>(); linkedListEmployees.add( new Employee(1, "Raj") ); linkedListEmployees.add( new Employee(2, "Maria") ); linkedListEmployees.add( new Employee(3, "John") ); linkedListEmployees.add( new Employee(4, "Emily") ); linkedListEmployees.add( new Employee(5, "May") ); System.out.println( linkedListEmployees.contains(new Employee(3, "John")) ); } } |
Output
1 |
false |
As you can see from the output, even if the linked list contains the specified object, the contains
method returned false.
Since our Employee class has not implemented the equals
method, the equals
method of the Object class was used to compare the objects. The equals
method of the Object class compares the object references which are different in our case and that is why the contains
method returned false.
Let’s implement the equals
and hashCode
methods in our Employee class and try again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.LinkedList; class Employee{ private Integer id; private String name; public Employee(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + " => " + this.name + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class LinkedListContainsExample { public static void main(String[] args) { LinkedList<Employee> linkedListEmployees = new LinkedList<Employee>(); linkedListEmployees.add( new Employee(1, "Raj") ); linkedListEmployees.add( new Employee(2, "Maria") ); linkedListEmployees.add( new Employee(3, "John") ); linkedListEmployees.add( new Employee(4, "Emily") ); linkedListEmployees.add( new Employee(5, "May") ); System.out.println( linkedListEmployees.contains(new Employee(3, "John")) ); } } |
Output
1 |
true |
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList