Get the size of a hash set

ReturnMethodSummary
intsize()Returns the number of elements in this set (its cardinality).

  import java.util.HashSet;

public class Main {
  public static void main(String args[]) {
    HashSet<String> hs = new HashSet<String>();

    hs.add("java2s.com");
    hs.add("A");
    hs.add("D");
    hs.add("E");
    hs.add("C");
    hs.add("j a v a2s.com");
    System.out.println(hs.size());
    hs.remove("A");
    System.out.println(hs.size());
  }
}

The output:


6
5
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.