Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java HashSet isEmpty() Method

Java HashSet isEmpty() Method

Java HashSet Java HashSet


isEmpty() The method is in Java's HashSet is a commonly used method provided by the class, used to determine whether the current collection is empty. This method belongs to java.util.HashSet class, inherited from java.util.AbstractCollection class.

Method Declaration

public

boolean

isEmpty

(

)

Return Value


Use Cases

isEmpty() The method is especially useful in the following scenarios:

1. Collection Initialization Check

After creating a new HashSet , you can use this method to check whether the collection is empty.

2. Validation Before Data Processing

Before operating on the collection, first check whether it is empty to avoid unnecessary operations.

3. Loop Termination Condition

can be used as the loop termination condition, exiting the loop when the collection is empty.


Example Code

Basic Usage Example

Example

import

java.util.HashSet

;

public

class

HashSetIsEmptyExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an empty HashSet

        HashSet

<
String
set
=
new
HashSet
<>
(
)

;

       

       

// Check whether the collection is empty

       

System

.

out

.

println

(

"Is the collection empty? "

set.

isEmpty

(

)

)

;

// Output: true

       

       

// Add elements

        set.

add

(

"Apple"

)

;

        set.

add

(

"Banana"

)

;

       

       

// Check again

       

System

.

out

.

println

(

"Is the collection empty? "

set.

isEmpty

(

)

)

;

// Output: false

   

}

}

Practical Application Examples

Example

import

java.util.HashSet

;

public

class

ProcessData

{

   

public

static

void

main

(

String

[

]

args

)

{

        HashSet

<
Integer
numbers
=
new
HashSet
<>
(
)

;

       

       

// Simulate data loading

        loadData

(

numbers

)

;

       

       

// Check before processing

       

if

(

!

numbers.

isEmpty

(

)

)

{

            processNumbers

(

numbers

)

;

       

}

else

{

           

System

.

out

.

println

(

"No data to process"

)

;

       

}

   

}

   

   

private

static

void

loadData

(

HashSet

<
Integer
set
)
{

// Here you could load data from a file or database

       

// In the example we do not add any data for now

   

}

   

   

private

static

void

processNumbers

(

HashSet

<
Integer
set
)
{
System

.

out

.

println

(

"Processing "

set.

size

(

)

" numbers..."

)

;

       

// Actual processing logic...

   

}

}


Notes

1. Difference from the size() Method

2. Performance Considerations

3. Concurrent Environment


Frequently Asked Questions

Q1: Does the isEmpty() method throw an exception?

No.isEmpty() is a simple query method that does not modify the collection and does not throw exceptions.

Q2: If the HashSet is null, what happens when calling isEmpty()?

will throw NullPointerException. You should check whether the reference is null before calling:

Example

if

(

set

!=

null

&&

set.

isEmpty

(

)

)

{

   

// Safe operation

}

Q3: Which has better performance, isEmpty() or size()?

In most implementations, both have comparable performance and are O(1) operations. The choice should be based on code readability rather than performance.


Summary

HashSet.isEmpty() is a simple but practical method that helps developers quickly determine the collection state. Using it correctly can:

  1. Improve code readability
  2. Avoid unnecessary collection operations
  3. Serve as a clear expression for conditional judgments

When writing code involving collection processing, use isEmpty() method appropriately can make the code more robust and easier to maintain.

Java HashSet Java HashSet