Get Size of HashSet - Java Collection Framework

Java examples for Collection Framework:HashSet

Description

Get Size of HashSet

Demo Code

 
import java.util.HashSet;
 
public class Main {
 
  public static void main(String[] args) {
    HashSet hSet = new HashSet();
    System.out.println("Size of HashSet : " + hSet.size());
   /*from w  w  w  .  j  a v a 2s .  c  o  m*/
    //add elements to HashSet object
    hSet.add(new Integer("1"));
    hSet.add(new Integer("2"));
    hSet.add(new Integer("3"));
 
    System.out.println("Size of HashSet after addition : " + hSet.size());
   
    //remove one element from HashSet using remove method
    hSet.remove(new Integer("1"));
    System.out.println("Size of HashSet after removal : " + hSet.size());
  }
}

Result


Related Tutorials