Java HashSet contains() Method
Example
Check if an item is in a set:
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.contains("BMW"));
System.out.println(cars.contains("Toyota"));
}
}
Definition and Usage
The contains() method checks if an item is in the set.
It returns true if the item is in the set, and false if it is not.
Syntax
public boolean contains(Object item)
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to look for. |
Technical Details
| Returns: | true if the item is in the set, and false if it is not. |
|---|