How to remove specific items or remove all items from a HashSet

How to remove specific items or remove all items from a HashSet:

Java HashSet provides methods to remove a specific element or to remove all elements. In this post, we will learn how to remove a specific item and how to remove all items from a Java HashSet.

Removing a specific item from Java HashSet:

To remove a specific element from a HashSet in Java, HashSet class provides a method called remove. Using this method, we can remove any element from a given HashSet.

This method is defined as below:

public boolean remove(Object e)

It takes one parameter and returns one boolean value.

  • The parameter is the item to remove.
  • It returns true if the removal is success. Else, it returns false.

e can be a null or non-null value. If it is null, this method will return true only if the HashSet contains null element.

Let’s take a look at the example below:

Example of Java HashSet remove:

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Integer valueToRemove = 3;

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

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

        if (hashSet.remove(valueToRemove)) {
            System.out.println(valueToRemove + " is removed");
        } else {
            System.out.println("Failed to remove !");
        }

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

Here,

  • valueToRemove is the value to remove from the hashset.
  • hashSet is the given HashSet. Initially, three numbers are added to this hashset.
  • We are printing the values in the HashSet before and after the item is removed.
  • The if condition checks for the return value of remove(). If it is true, i.e. removal was successfull, it prints one message that the value is removed. Else, it moves to the else block and prints a different message that the removal failed.
  • Finally, it prints the HashSet values again.

If you run the above program, it will print the below output:

Given HashSet :
1
2
3
3 is removed
Current HashSet :
1
2

As you can see here, since 3 is in the hashset, it is removed and the final HashSet contains only two values.

If you try to remove any different element which is not in the hashSet, which you can do by changing the value of valueToRemove, it will print the message in the else block.

Example of removing a null value:

We can pass null to this method and it will work if the HashSet holds any null value. If it doesn’t have any null value, it will return false, else it will return true.

Let me change the above example with a null element:

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Integer valueToRemove = null;

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

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

        if (hashSet.remove(valueToRemove)) {
            System.out.println(valueToRemove + " is removed");
        } else {
            System.out.println("Failed to remove !");
        }

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

It is similar to the above program. The only difference is that we are removing null from the HashSet. It will print the below output:

Given HashSet :
null
1
2
null is removed
Current HashSet :
1
2

Remove all element from a HashSet:

To remove all elements from a HashSet in Java, we can use the clear() method. This method removes all elements from the given hashset.

It is defined as below:

public void clear()

It doesn’t return any value. It just cleans the HashSet.

For example:

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Integer valueToRemove = null;

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

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

        hashSet.clear();

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

For this program, it will print the below output:

Given HashSet :
1
2
3
Current HashSet :

All items are removed from the HashSet.

You might also like: