HashSet<E> · Java
contains
Tests membership.
public boolean contains(Object o)
Complexity
| Expected | O(1) |
|---|---|
| Worst case | O(n) |
| Extra space | O(1) |
HashMap.containsKey.
Tested example
Set<String> words = new HashSet<>(List.of("apple", "pear"));
assert words.contains("apple"); // hashCode(), then equals()
assert !words.contains("Apple");
Parameters
- o
- The element.
Behaviour
- Returns
- Whether it is present.
- Errors and preconditions
- None.
- Changes
- Nothing.
- Memory and invalidation
- No allocation.