HashSet Properties:
✓ The underlying data structure of HashSet is HashTable.
✓HashSet implements Set, Cloneable, Serializable interfaces.
✓The initial capacity of HashSet is 16.
✓ It doesn't maintain the insertion order. Here elements are inserted on the basis of their hashcode.
✓HashSet contains unique elements only.
✓ It allows only one null value
ex:
public class HashSet {
public static void main(String[] args) {
HashSet h = new HashSet();
h.add(10);
h.add(10);
h.add(null);
h.add("Java");
h.add(null);
for(Object obj : h) {
System.out.println(obj);
}
}
}
Output:
Java
10
null
NOTE:
Even if we add duplicate values in Set also, it won't throw any exception. Just it will override the existing value with new value and display only one value.