Java Collection Tutorial - Java HashSet.size()








Syntax

HashSet.size() has the following syntax.

public int size()

Example

In the following code shows how to use HashSet.size() method.

/*from w w  w  .  j  a va  2  s  . c om*/
import java.util.HashSet;

public class Main {
   public static void main(String args[]) {
      HashSet<String>  newset = new HashSet <String> ();
                  
      // populate hash set
      newset.add("Learning"); 
      newset.add("from");
      newset.add("java2s.com");  
      
      System.out.println("Size of the set: "+newset.size());     
   }    
}

The code above generates the following result.