How to get the size of java Hashset
Previous | Home | Next |
This Examples shows how the size of Hashset is get to know.Hashset class of Java Collection has a method Size() to know the size of the Hashset.
Size () of Hashset class of java collection is used in this program.Size() returns the size of the Hashset elements.
The Size() method of Hashset class of Java collection is predefined method it can pass with the object of Hashset .
Size() method of Hashset class is alled with the help of object HSet.
It will print the size of the HashSet the before populated Hashset and after populated Hashset.
package Example;
import java.util.HashSet;
public class Size {
public static void main(String[] args) {
HashSet HSet = new HashSet();
System.out.println("The size of hash set before adding elements :" + HSet.size());
HSet.add(new Integer("11"));
HSet.add(new Integer("22"));
System.out.println("Size of HashSet after addition : " + HSet.size());
HSet.add(new Integer("33"));
System.out.println("Size of HashSet after adding one more element : " + HSet.size());
}
}
The size of hash set before adding elements :0
Size of HashSet after addition : 2
Size of HashSet after adding one more element : 3
Previous | Home | Next |