The contains(E element) method of LinkedList checks if the LinkedList contains the specified element or not. It returns true if the linked list contains the specified element. Show Syntax boolean contains(E e)Parameter e is the element to check in the linked-list
Returns Value It returns true if the linked-list contains specified element otherwise returns falseProgram import java.util.LinkedList; public class LinkedListContainsExample { public static void main(String[] args) { LinkedList<Integer> oddNumbers = new LinkedList<>(); oddNumbers.add(5); oddNumbers.add(7); oddNumbers.add(9); oddNumbers.add(11); System.out.println("Linked List elements: " + oddNumbers); System.out.println("Does this linked-list contains 7: " + oddNumbers.contains(7)); System.out.println("Does this linked-list contains 4: " + oddNumbers.contains(4)); } }Output Linked List elements: [5, 7, 9, 11] Does this linked-list contains 7: true Does this linked-list contains 4: false
By Yashwant Chavan, Views 4150, Last updated on 26-Nov-2016 In this tutorial, you will learn about Java LinkedList.contains() method, which returns true if this list contains the specified element. tags java
Returns true if this list contains the specified element boolean java.util.LinkedList.contains(Object o)Parametero element whose presence in this list is to be tested. Returntrue if this list contains the specified element otherwise returns false value. Java LinkedList.contains(Object o) ExampleRefer below steps
OutputLinkedList contains Pune ? false LinkedList contains Delhi ? trueSuggested Posts
Yashwant Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com] 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.
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.
Output 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.
Output 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.
Output This example is a part of the LinkedList in Java tutorial. Please let me know your views in the comments section below. References: |