![]() |
VOOZH | about |
Set.contains() method in Java is used to check if a specific element exists in a Set. It returns true if the element is present, otherwise false. This method is useful for quickly verifying membership in collections that do not allow duplicates, like HashSet or TreeSet.
Example:
Does the set contain Apple?: true Does the set contain Orange?: false
Explanation:
boolean contains(Object element)
Example:This code shows how Set.contains() checks for an element in a HashSet.
Set: [4, Geeks, Welcome, To] Does the Set contain 'Geeks'?: true Does the Set contain '4'?: true Does the Set contain 'No'?: false
Explanation:
Note:Works for all Set types (HashSet, TreeSet, LinkedHashSet), has O(1) time complexity for HashSet.contains().