Java Hashset Size
# Java HashSet size() Method
[ Java HashSet](#)
* * *
The `size()` method is a built-in method in Java's `HashSet` class used to return the number of elements currently in the collection. Its method signature is as follows:
### Method Syntax
public int size()
### Return Value
This method returns an `int` value representing the current number of elements stored in the `HashSet` instance.
### Important Notes
* The return value is always a non-negative integer
* Returns 0 when the collection is empty
* The return value reflects the collection size at the moment of calling; subsequent add/remove operations will change this value
* * *
## Usage Examples
### Basic Usage
## Example
import java.util.HashSet;
public class HashSetSizeExample {
public static void main(String[] args){
// Create a new HashSet
HashSet fruits =new HashSet();
// Add elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Use the size() method to get the collection size
int size = fruits.size();
System.out.println("Number of elements in HashSet: "+ size);// Output: 3
}
}
### Empty Collection Case
## Example
HashSet numbers =new HashSet();
System.out.println("Size of empty HashSet: "+ numbers.s
YouTip