This example shows how to add elements to a HashSet object in Java. This example also shows how to add elements using the add method and addAll method of the HashSet class.
How to add elements to HashSet in Java using the add method?
The add
method of the HashSet class adds the specified element to this HashSet object.
1 |
public boolean add(E e) |
The add
method adds the specified element only if it is not already present in the set object. It returns true if the specified element was added to the set and returns false if the element was already present in the set object and was not added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.HashSet; public class HashSetAddElementsExample { public static void main(String[] args) { HashSet<Integer> hsetNumbers = new HashSet<Integer>(); /* * To add elements to a HashSet object, use * the add method. */ /* * This will add 1 to the HashSet object and * return true. */ System.out.println( hsetNumbers.add(1) ); /* * This will not add 1 to the HashSet object and * return false because 1 is already present in the set. */ System.out.println( hsetNumbers.add(1) ); /* * This will add null to the HashSet and return true */ System.out.println( hsetNumbers.add(null) ); /* * This will not add null to the HashSet * and return false because null is already present * in the set object. */ System.out.println( hsetNumbers.add(null) ); System.out.println("HashSet contains: " + hsetNumbers); } } |
Output
1 2 3 4 5 |
true false true false HashSet contains: [null, 1] |
Note: The HashSet class allows a null element to be added to it. However, since the set is a collection of unique elements, we cannot add more than one null element to the HashSet object.
How to add custom class objects to HashSet using the add method?
The add
method uses equals
and hashCode
methods to check if the element already exists in the HashSet object. If the element is an object of the custom class, the custom class needs to override these methods. Let’s see what happens when we do not do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashSet; class Emp{ private int empId; public Emp(int empId){ this.empId = empId; } public String toString(){ return "Emp: " + this.empId; } } public class HashSetAddElementsExample { public static void main(String[] args) { HashSet<Emp> hsetEmployees = new HashSet<Emp>(); System.out.println( hsetEmployees.add(new Emp(1)) ); System.out.println( hsetEmployees.add(new Emp(1)) ); System.out.println("HashSet contains: " + hsetEmployees); } } |
Output
1 2 3 |
true true HashSet contains: [Emp: 1, Emp: 1] |
As we can see from the output, duplicate objects were added to the HashSet using the add
method. Since our Emp custom class has not overridden the equals
and hashCode
methods, methods inherited from the Object class were used which compare the references, not the actual object contents.
In order to solve duplicate objects being added to the HashSet problem, we need to override the equals
and hashCode
methods in our custom class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import java.util.HashSet; class Emp{ private int empId; public Emp(int empId){ this.empId = empId; } public String toString(){ return "Emp: " + this.empId; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + empId; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Emp other = (Emp) obj; if (empId != other.empId) return false; return true; } } public class HashSetAddElementsExample { public static void main(String[] args) { HashSet<Emp> hsetEmployees = new HashSet<Emp>(); System.out.println( hsetEmployees.add(new Emp(1)) ); System.out.println( hsetEmployees.add(new Emp(1)) ); System.out.println("HashSet contains: " + hsetEmployees); } } |
Output
1 2 3 |
true false HashSet contains: [Emp: 1] |
As we can see from the output, the add
method returned false for the duplicate object this time and the object was not added to the HashSet.
How to add all elements of another collection to HashSet using the addAll method?
The addAll
method of the HashSet class adds all elements of the specified collection to this HashSet object.
1 |
boolean addAll(Collection<? extends E> collection) |
The addAll
method adds all elements from the specified collection object only if they are not already present. It returns true if the set was changed as a result of this method call, false otherwise.
Below given example shows how to add all elements from ArrayList to HashSet using the addAll
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
HashSet<Integer> hsetNumbers = new HashSet<Integer>(); hsetNumbers.add(1); hsetNumbers.add(2); hsetNumbers.add(3); List<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(3); aListNumbers.add(4); aListNumbers.add(5); /* * To add all elements of other collection to the HashSet, use * the addAll method */ //this will add all elements of ArrayList to HashSet System.out.println( hsetNumbers.addAll(aListNumbers) ); System.out.println("HashSet contains: " + hsetNumbers); |
Output
1 2 |
true HashSet contains: [1, 2, 3, 4, 5] |
As we can see from the output, the ArrayList element 3 was not added to the HashSet because it was already present. All other elements were added.
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