Skip to main content

Write a java program to get the number of elements in a hash set.

· One min read
Kaustubh Kulkarni
numberofElements.java
import java.util.*;  
public class numberofElements {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("A");
hashSet.add("B");
System.out.println("Hashset size is "+hashSet.size());
hashSet.add("C");
System.out.println("Hashset size is "+hashSet.size());
}
}

Output:

  

Hashset size is 2
Hashset size is 3

View on Your Phone