Java HashSet size() Method
Example
Get the number of items 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.size());
}
}
Definition and Usage
The size() method returns the number of items in the set.
Syntax
public int size()
Parameter Values
None.
Technical Details
| Returns: | The number of items in the set. |
|---|