This example shows how to check if the HashSet is empty in Java. The example also shows how to check using the isEmpty method and size method of the HashSet class.
How to check if HashSet is empty in Java?
There are a couple of ways using which you can check if the HashSet object is empty as given below.
1. Using the isEmpty method
The isEmpty
method of the HashSet class returns true if there are no elements in the HashSet object.
1 |
public boolean isEmpty() |
The isEmpty
method returns false if there is at least one element in the HashSet object.
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 |
import java.util.HashSet; public class HashSetIsEmptyExample { public static void main(String[] args) { //create new empty HashSet HashSet<String> hsetColors = new HashSet<String>(); /* * To check if the HashSet object is empty or not, use * the isEmpty method */ //this will return true as the set is empty System.out.println("HashSet empty: " + hsetColors.isEmpty()); //add some elements hsetColors.add("Red"); hsetColors.add("Green"); //this will return false as there are two elements in the set System.out.println("HashSet empty: " + hsetColors.isEmpty()); } } |
Output
1 2 |
HashSet empty: true HashSet empty: false |
2. Using the size method
You can also use the size
method of the HashSet class to check the same.
1 |
public int size() |
If the HashSet size is 0 then the HashSet is empty, otherwise not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//create new empty HashSet HashSet<String> hsetColors = new HashSet<String>(); /* * You can also use the size method and compare the * return value with 0 to check the same. */ //this will return true as the set is empty System.out.println("HashSet empty: " + (hsetColors.size() == 0)); //add some elements hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); //this will return false as there are three elements in the set System.out.println("HashSet empty: " + (hsetColors.size() == 0)); |
Output
1 2 |
HashSet empty: true HashSet empty: false |
Which is the preferred way to check?
Let’s have a look at the Java 8 HashSet source code for both of these methods.
1 2 3 4 5 6 7 8 |
/** * Returns the number of elements in this set (its cardinality). * * @return the number of elements in this set (its cardinality) */ public int size() { return map.size(); } |
1 2 3 4 5 6 7 8 |
/** * Returns <tt>true</tt> if this set contains no elements. * * @return <tt>true</tt> if this set contains no elements */ public boolean isEmpty() { return map.isEmpty(); } |
As we can see from the source code, both of these methods call the internal HashMap object’s isEmpty
and size
methods.
The HashMap isEmpty
and size
methods refer to the same internal size variable. So as far as performance is concerned, both of these methods will perform similarly.
However, the isEmpty
method will make the code cleaner and more readable as compared to getting the size and then comparing it with the 0. That is the reason using the isEmpty
method is the preferred way to check.
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