Mega Code Archive

 
Categories / Java Tutorial / Collections
 

Creating a HashSet

The HashSet class provides four constructors broken into two sets. The first three constructors create empty sets of varying sizes: public HashSet() public HashSet(int initialCapacity) public HashSet(int initialCapacity, int loadFactor) When creating any collection, it is always best to have the local variable be of the interface type, Set set = new HashSet(). For easier to change the set to a TreeSet or some other set implementation. The fourth constructor acts as a copy constructor. public HashSet(Collection col) If the original collection had duplicates, only one of the duplicates will be in the final created set.