How to Get Size of a HashSet in Java


In this example we will show you how to get the size of a HashSet in Java.

Codes

package com.beginner.examples;

import java.util.HashSet;

public class GetSizeOfHashSet {
  public static void main(String[] args) {
    // Create a HashSet object
    HashSet sets = new HashSet();
    sets.add("a");
    sets.add("b");
    sets.add("c");
    System.out.println(sets.size()); // //get the size of a HashSet
  }
}

Output:

3

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments