The LinkedList.contains()
method in Java is used to check if a specified element is present in the LinkedList
. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. Additionally, we will cover a real-world use case to illustrate its application.
Table of Contents
- Introduction
contains
Method Syntax- How It Works
- Examples
- Checking if a LinkedList Contains an Element
- Handling Non-Existing Elements
- Real-World Use Case
- Conclusion
Introduction
The LinkedList.contains()
method is part of the LinkedList
class in Java. It allows you to determine whether a specific element is present in the linked list. This method is useful for searching and verifying the existence of elements within a list.
contains Method Syntax
The syntax for the contains
method is as follows:
public boolean contains(Object o)
- o: The element whose presence in the list is to be tested.
- The method returns
true
if the list contains the specified element, andfalse
otherwise.
How It Works
When you use the contains(Object o)
method, the LinkedList
iterates through its elements, comparing each element with the specified element using the equals()
method. If a match is found, the method returns true
. If no match is found after checking all elements, the method returns false
.
Examples
Checking if a LinkedList Contains an Element
The contains
method can be used to check if a specific element is present in the LinkedList
.
Example
import java.util.LinkedList;
public class ContainsExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Check if the list contains "Banana"
boolean containsBanana = list.contains("Banana");
System.out.println("Does the list contain 'Banana'? " + containsBanana);
}
}
Output:
Does the list contain 'Banana'? true
Handling Non-Existing Elements
If the specified element is not present in the LinkedList
, the contains
method will return false
.
Example
import java.util.LinkedList;
public class ContainsNotFoundExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Check if the list contains "Grapes"
boolean containsGrapes = list.contains("Grapes");
System.out.println("Does the list contain 'Grapes'? " + containsGrapes);
}
}
Output:
Does the list contain 'Grapes'? false
Real-World Use Case
Checking User Permissions
In an application where you manage user permissions, you might store the permissions in a LinkedList
. The contains()
method can be used to verify if a user has a specific permission.
Example
import java.util.LinkedList;
class User {
String name;
LinkedList<String> permissions;
User(String name) {
this.name = name;
this.permissions = new LinkedList<>();
}
void addPermission(String permission) {
permissions.add(permission);
}
boolean hasPermission(String permission) {
return permissions.contains(permission);
}
@Override
public String toString() {
return name + " with permissions: " + permissions;
}
}
public class UserPermissions {
public static void main(String[] args) {
User user = new User("Alice");
user.addPermission("READ");
user.addPermission("WRITE");
System.out.println(user);
// Check if the user has "EXECUTE" permission
boolean hasExecutePermission = user.hasPermission("EXECUTE");
System.out.println("Does Alice have 'EXECUTE' permission? " + hasExecutePermission);
}
}
Output:
Alice with permissions: [READ, WRITE]
Does Alice have 'EXECUTE' permission? false
Conclusion
The LinkedList.contains()
method in Java provides a simple way to check if a specific element is present in a LinkedList
. By understanding how to use this method, you can efficiently search and verify elements within your linked lists in Java applications. This method is particularly useful in scenarios such as checking user permissions, searching for specific data, and verifying the presence of elements in collections.