YouTip LogoYouTip

Java Linkedlist Contains

[![Image 1: Java LinkedList](#) Java LinkedList](#)\\n\\n* * *\\n\\nThe `contains()` method is a common method provided by the `LinkedList` class in Java, used to check whether the linked list contains a specified element. This method belongs to the `java.util.LinkedList` class and inherits from the `java.util.AbstractCollection` class.\\n\\n### Method Syntax\\n\\npublic boolean contains(Object o)\\n### Return Value\\n\\n* If the linked list contains the specified element, it returns `true`\\n* If the specified element is not contained, it returns `false`\\n\\n* * *\\n\\n## Method Details\\n\\n### Working Principle\\n\\nThe `contains()` method traverses each element in the linked list and uses the `equals()` method to compare each element with the target object. If a matching element is found, it immediately returns `true`; if no matching element is found after traversing the entire linked list, it returns `false`.\\n\\n### Time Complexity\\n\\nSince `LinkedList` is a doubly linked list, the `contains()` method needs to search sequentially from the head of the list. Therefore, its time complexity is O(n), where n is the number of elements in the linked list.\\n\\n* * *\\n\\n## Usage Examples\\n\\n### Basic Usage\\n\\n## Example\\n\\nimport java.util.LinkedList;\\n\\npublic class LinkedListContainsExample {\\n\\npublic static void main(String[] args){\\n\\n// Create a LinkedList\\n\\n LinkedList fruits =new LinkedList();\\n\\n// Add element\\n\\n fruits.add("Apple");\\n\\n fruits.add("Banana");\\n\\n fruits.add("Orange");\\n\\n// Check if Element Exists\\n\\nSystem.out.println("Contains Apple? "+ fruits.contains("Apple"));// Output: true\\n\\nSystem.out.println("Contains Grape? "+ fruits.contains("Grape"));// Output: false\\n\\n}\\n\\n}\\n\\n### Using Custom Objects\\n\\nWhen using custom objects, you need to properly override the `equals()` method, otherwise the `contains()` method may not work as expected.\\n\\n## Example\\n\\nimport java.util.LinkedList;\\n\\nclass Person {\\n\\nprivate String name;\\n\\nprivate int age;\\n\\npublic Person(String name, int age){\\n\\nthis.name= name;\\n\\nthis.age= age;\\n\\n}\\n\\n// Override equals Method\\n\\n @Override\\n\\npublic boolean equals(Object obj){\\n\\nif(this== obj)return true;\\n\\nif(obj ==null|| getClass()!= obj.getClass())return false;\\n\\n Person person =(Person) obj;\\n\\nreturn age == person.age&& name.equals(person.name);\\n\\n}\\n\\n// Override hashCode Method\\n\\n @Override\\n\\npublic int hashCode(){\\n\\nreturn 31* name.hashCode()+ age;\\n\\n}\\n\\n}\\n\\npublic class CustomObjectExample {\\n\\npublic static void main(String[] args){\\n\\n LinkedList people =new LinkedList();\\n\\n people.add(new Person("Alice", 25));\\n\\n people.add(new Person("Bob", 30));\\n\\nPerson alice =new Person("Alice", 25);\\n\\nSystem.out.println("Contains Alice? "+ people.contains(alice));// Output: true\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Notes\\n\\n### 1. Handling null Values\\n\\n`LinkedList` allows null values, and you can use `contains(null)` to check whether the linked list contains null values.\\n\\n## Example\\n\\nLinkedList list =new LinkedList();\\n\\n list.add(null);\\n\\nSystem.out.println(list.contains(null));// Output: true\\n\\n### 2. Performance Considerations\\n\\nSince the `contains()` method needs to traverse the linked list, frequently calling this method on large linked lists may affect performance. If you need to frequently check whether elements exist, consider using data structures more suitable for searching, such as `HashSet`.\\n\\n### 3. Importance of the equals() Method\\n\\nThe `contains()` method relies on the element's `equals()` method for object comparison. If a custom class does not correctly override the `equals()` method, the `contains()` method may not correctly identify equal objects.\\n\\n* * *\\n\\n## Comparison with Other Methods\\n\\n### contains() vs indexOf()\\n\\n* `contains()`: Returns boolean, only checks whether the element exists\\n* `indexOf()`: Returns int, returns the index position of the first occurrence of the element, or returns -1 if not found\\n\\n## Example\\n\\nLinkedList list =new LinkedList();\\n\\n list.add("A");\\n\\n list.add("B");\\n\\n list.add("A");\\n\\nSystem.out.println(list.contains("A"));// Output: true\\n\\nSystem.out.println(list.indexOf("A"));// Output: 0\\n\\n### contains() vs containsAll()\\n\\n* `contains()`: Checks whether a single element exists\\n* `containsAll()`: Checks whether all elements in the specified collection are contained\\n\\n## Example\\n\\nLinkedList list =new LinkedList();\\n\\n list.add("A");\\n\\n list.add("B");\\n\\n list.add("C");\\n\\nSystem.out.println(list.contains("A"));// Output: true\\n\\nList checkList =Arrays.asList("A", "B");\\n\\nSystem.out.println(list.containsAll(checkList));// Output: true\\n\\n* * *\\n\\n## Summary\\n\\nThe `contains()` method of `LinkedList` is a simple but useful tool for checking whether a specific element exists in a linked list. Understanding its working principle and correct usage is very important for writing efficient Java programs. Keep the following points in mind:\\n\\n1. The `contains()` method uses `equals()` for element comparison\\n2. For custom objects, you
← Java Linkedlist GetfirstJava Linkedlist Remove β†’