HashSet size() in Java

size(): This method is available in java.util.HashSet class of Java.

Syntax:

int java.util.HashSet.size()

This method returns the number of elements in this set (its cardinality).

Parameters: NA

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

Exceptions: NA

Approach

Java

import java.util.HashSet;

public class HashSetsize {
    public static void main(String[] args) {

        HashSet<String> hashSet = new HashSet<String>();

        hashSet.add("Hello");
        hashSet.add("Java");

        System.out.println(hashSet.size());
    }
}

Output:

2


No comments:

Post a Comment