Java HashSet Add Elements Example

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

publicbooleanadd(Ee)

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

importjava.util.HashSet;

publicclassHashSetAddElementsExample{

publicstaticvoidmain(String[]args){

HashSet<Integer>hsetNumbers=newHashSet<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

importjava.util.HashSet;

classEmp{

privateintempId;

publicEmp(intempId){

this.empId=empId;

}

publicStringtoString(){

return"Emp: "+this.empId;

}

}

publicclassHashSetAddElementsExample{

publicstaticvoidmain(String[]args){

HashSet<Emp>hsetEmployees=newHashSet<Emp>();

System.out.println(hsetEmployees.add(newEmp(1)));

System.out.println(hsetEmployees.add(newEmp(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

importjava.util.HashSet;

classEmp{

privateintempId;

publicEmp(intempId){

this.empId=empId;

}

publicStringtoString(){

return"Emp: "+this.empId;

}

publicinthashCode(){

finalintprime=31;

intresult=1;

result=prime*result+empId;

returnresult;

}

publicbooleanequals(Objectobj){

if(this==obj)

returntrue;

if(obj==null)

returnfalse;

if(getClass()!=obj.getClass())

returnfalse;

Emp other=(Emp)obj;

if(empId!=other.empId)

returnfalse;

returntrue;

}

}

publicclassHashSetAddElementsExample{

publicstaticvoidmain(String[]args){

HashSet<Emp>hsetEmployees=newHashSet<Emp>();

System.out.println(hsetEmployees.add(newEmp(1)));

System.out.println(hsetEmployees.add(newEmp(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

booleanaddAll(Collection<?extendsE>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=newHashSet<Integer>();

hsetNumbers.add(1);

hsetNumbers.add(2);

hsetNumbers.add(3);

List<Integer>aListNumbers=newArrayList<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.

About the author

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.