HashSet in Java and what is FillRatio or LoadFactor?
- The underlying data-structure is Hashtable.
- Duplicate objects are not allowed.
- Insertion order is not preserved and it is based on HashCode of objects.
- Null insertionÂ is possible (only once).
- Heterogeneous objects are allowed.
- Implements serializable and cloneable but not the RandomAccess interface.
- HashSet is the best choice if our frequent operation is search operation.
- HashSet is not thread-safe. If multiple threads try to modify a HashSet at the same time, then the final outcome is not-deterministic. You must explicitly synchronize concurrent access to a HashSet in a multi-threaded environment.
- HashSet iterator methods are fail-fast. So any structural modification to the set after the creation of iterator will throw ConcurrentModificationException.
- Note:-Â In HashSet duplicate are not allowed if we are trying to insert duplicates then we won’t get any compile time or runtime errors and add method simply returns false.
|1||HashSet h=new HashSet()||Creates an empty HashSet object with default initial capacity 16 and default ratio 0.75.|
|2||HashSet h=new HashSet(int initial capacity()||Creates an empty HashSet object with specified initial capacity and default ratio 0.75.|
|3||HashSet h=new HashSet(int initial capacity, float FillRatio)|
|4||HashSet h=new HashSet(Collection c)||Creates an equivalent HashSet for the given collection. This constructor meant for inter-conversion between collection object.|