This example shows how to create a new HashSet object in Java. This example also shows how to use various HashSet constructors that allow us to create new HashSet objects.
How to create new HashSet objects in Java using constructors?
The HashSet class in Java provides several constructors using which we can create new objects as given below.
How to create a new empty HashSet object?
The default HashSet constructor creates a new and empty HashSet object.
1 |
public HashSet() |
The HashSet object created using this constructor has the initial capacity of 16 (i.e. default capacity) and a load factor of 0.75.
1 2 3 4 5 |
/* * This will create new HashSet object with * initial capacity of 16 and load factor of 0.75 */ HashSet<Integer> hsetNumbers = new HashSet(); |
If you want to specify the initial capacity while creating an object, you can use below given overloaded constructor that accepts the initial capacity argument.
1 |
public HashSet(int initialCapacity) |
The HashSet object created using this constructor has the specified initial capacity and default load factor i.e. 0.75.
1 2 3 4 5 |
/* * This will create new and empty HashSet object with * initial capacity of 25 and load factor of 0.75 */ HashSet<Integer> hsetNumbers = new HashSet(25); |
If you want to specify both, the initial capacity and the load factor, you can use below given overloaded constructor.
1 |
public HashSet(int initialCapacity, float loadFactor) |
The HashSet object created using this constructor has the specified initial capacity and load factor.
1 2 3 4 5 |
/* * This will create new and empty HashSet object with * initial capacity of 25 and load factor of 0.80 */ HashSet<Integer> hsetNumbers = new HashSet(25, 0.80f); |
How to create a HashSet object from another collection object?
The HashSet class provides a constructor that accepts a Collection object as an argument.
1 |
public HashSet(Collection<? extends E> collection) |
This constructor creates a new HashSet object containing all elements of the specified collection object. The new set object has the default load factor i.e. 0.75 and initial capacity big enough to accommodate all the elements of the specified collection object.
The below given example shows how to create HashSet from an ArrayList object using this constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ArrayList<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); aListNumbers.add(1); /* * To create a HashSet object from the ArrayList, * use the below given constructor. */ HashSet<Integer> hsetNumbers = new HashSet<Integer>( aListNumbers ); System.out.println("HashSet contains: " + hsetNumbers); |
Output
1 |
HashSet contains: [1, 2, 3] |
As you can see from the output, the element “1” was added only once in the HashSet as the HashSet class does not allow duplicate elements. You can also convert LinkedList to the HashSet in the same way.
This example is a part of the Java HashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet