returns the number of elements in this set(its cardinality).
Constructors in hashset
HashSet h = new HashSet();
This constructor creates an empty hashset object with default initial capacity 16 and default fill ratio or load factor 0.75
HashSet h = new HashSet(int initialCapacity);
This constructor creates hashset object with specified initialCapacity with load factor 0.75. Nothing but customized initial capacity and default load factor.
HashSet h = new HashSet(int initialCapacity, float loadFactor);
Here we can customize load factor.
HashSet h = new HashSet(Collection C);
If you want Interconversion between Collection object then use this constructor.
Iterate hashset in java
To iterate hashset in java we have to use for-each loop.
import java.util.HashSet;
public class HashSetForEach
{
public static void main(String[] args)
{
HashSet<String> hs = new HashSet<String>();
hs.add("Hello");
hs.add("world");
hs.add("Java");
for(String str : hs)
{
System.out.println(str);
}
}
}