in conclusion:List
ofcontains()
The method is used to check whether the specified element is included in the list and judge with the help of the equals() method. If this element exists in the list, returntrue
; otherwise returnfalse
。
Expand in detail
1. Method signature
contains()
yesA method in the interface is defined as follows:
boolean contains(Object o)
- Parameters
o
: The target object to be checked. - Return value: If the list contains the specified element, return
true
; otherwise returnfalse
。
2. Working principle
contains()
The method will call theequals()
Method, compare with the incoming object. If a matching element is found (equals()
returntrue
), then returntrue
; otherwise returnfalse
。
3. Usage example
Example 1: Basic usage
import ; import ; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); ("Apple"); ("Banana"); ("Orange"); // Check if "Banana" is included in the list boolean containsBanana = ("Banana"); (containsBanana); // Output: true // Check if the list contains "Grapes" boolean containsGrapes = ("Grapes"); (containsGrapes); // Output: false } }
Example 2: Custom object'sequals()
method
If the list is stored with a custom object, you need to make sure it is rewrite correctly.equals()
Methods so thatcontains()
Can work normally.
import ; import ; class Person { private String name; public Person(String name) { = name; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != ()) return false; Person person = (Person) obj; return (); } @Override public int hashCode() { return (); } } public class Main { public static void main(String[] args) { List<Person> list = new ArrayList<>(); (new Person("Alice")); (new Person("Bob")); // Check if "Alice" is included in the list boolean containsAlice = (new Person("Alice")); (containsAlice); // Output: true // Check if the list contains "Charlie" boolean containsCharlie = (new Person("Charlie")); (containsCharlie); // Output: false } }
Note: If not rewriteequals()
method,contains()
Comparison will be made based on the reference address of the object, not the content.
4. Things to note
Null value check:
- If the list contains
null
, and callcontains(null)
, will returntrue
。 - Example:
List<String> list = new ArrayList<>(); (null); ((null)); // Output: true
Performance issues:
- For
ArrayList
andLinkedList
and other implementation categories,contains()
The time complexity of the method is O(n), because it requires iterating through the entire list. - If you need to frequently look up elements, you can consider using
HashSet
orTreeSet
etc.
Thread Safety:
- If used in a multi-threaded environment
List
, concurrency problems may occur. Can be used()
Package list to ensure thread safety.
Summarize
()
Method is a very practical tool to determine whether a certain element is included in the list. The following points should be paid attention to when using:
- Ensure that the target object is correctly implemented
equals()
Methods (especially custom objects). - For large lists, be aware of performance issues.
- In a multithreaded environment, make sure the list is thread-safe.
This is the end of this article about the use of List contains() method in Java. For more related Java List contains() content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!