HashSet size() method in java

Let’s learn HashSet size() method in java.

HashSet size() method in java

size() method HashSet class returns the number of elements in this set.

Syntax:

public int size()

Parameters:

size() method do not take any parameter.

Now let’s see example on HashSet size() method.

import java.util.HashSet;
public class HashSetSizeMethodExample
{
   public static void main(String[] args)
   {
      HashSet<String> hs = new HashSet<String>();
      hs.add("Welcome");
      hs.add("hello");
      hs.add("world");
      hs.add("core");
      hs.add("java");
      System.out.println("HashSet values: " + hs);
      System.out.println("The size of HashSet is: " + hs.size());
   }
}

Output:

HashSet values: [core, world, java, Welcome, hello]
The size of HashSet is: 5


Also read – abstraction in java