How to add items to a HashSet in Java

How to add items to a HashSet in Java:

To add items to a HashSet in Java, the HashSet class provides a method called add. We can use it to add a new item to its existing items.

In this post, we will learn how this method works with different examples.

HashSet add method definition:

add method is defined as below:

public boolean add(E elem)

This instance method takes the element as its parameter and returns one boolean value. This boolean value defines wheather the addition is success or not.

It adds one element first to the set if there is no element second such that (first == null ? second == null : first.equals(second)). So, if the set already has a null element and we are trying to add another null, it will return false. Also, if it already has the same value, it will not add another equal value and return false.

Let’s try it with different examples.

HashSet add method example:

Let’s take a look at the program below:

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {

        HashSet<Integer> hashSet = new HashSet<Integer>() {{
            add(1);
            add(2);
            add(3);
        }};

        if(hashSet.add(4)){
            System.out.println("New value added");
        }else{
            System.out.println("Failed to add a new value !");
        }

        System.out.println("Current HashSet :");
        hashSet.forEach(System.out::println);
    }
}

Here,

  • hashSet is a HashSet with three values added to it.
  • We are trying to add a new value to this HashSet in a if-else block. This block checks the return value of this method. If it is true, i.e. if the addition is successfull, it prints one message. If it fails to add a value, it prints another message in the else block.
  • After the if-else block, it prints the complete HashSet.

If you run the above program, since we don’t have 4 in the hashSet initially, the addition will be successfull and it will print one output as like below:

New value added
Current HashSet :
1
2
3
4

If you try to add an existing value like 3, add will return false and it will move to the else block and print one result like below:

Failed to add a new value !
Current HashSet :
1
2
3

Adding null values to a HashSet:

We can also add null values to a HashSet. If the HashSet doesn’t have any null value, then add will return true. Else, it will return false.

Let’s take a look at the below program:

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {

        HashSet<Integer> hashSet = new HashSet<Integer>() {{
            add(1);
            add(2);
            add(3);
        }};

        if(hashSet.add(null)){
            System.out.println("New value added");
        }else{
            System.out.println("Failed to add a new value !");
        }

        System.out.println("Current HashSet :");
        hashSet.forEach(System.out::println);
    }
}

Here,

  • hashSet is the given HashSet with three numbers.
  • Using add, we are trying to add null to this HashSet. Since we don’t have any null value initially, it will work and add will return true. It will move in the if block and print the below output:
New value added
Current HashSet :
null
1
2
3

Now, if we already have one null value initially, add method will return false in that case the program will print Failed to add a new value !.

You might also like: