Java HashSet clear() Method
clear() The method is in Java's HashSet is a commonly used method provided by the class, used to remove all elements from the collection. After calling this method, the collection becomes an empty set.
Method Signature:
Method Syntax
public
void
clear
(
)
Method Functionality
clear() The main functions of the method are:
- Removes the
HashSetall elements in the - reset the collection size to 0
- does not change the collection's capacity, only clears the content
Usage examples
Basic Usage Example
Example
import
java.util.HashSet
;
public
class
HashSetClearExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a HashSet and add elements
HashSet
<
String
fruits
=
new
HashSet
<>
(
);
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Orange"
)
;
System
.
out
.
println
(
"Set before clearing: "
fruits
)
;
System
.
out
.
println
(
"Size before clearing: "
fruits.
size
(
)
)
;
// Use the clear() method to clear the collection
fruits.
clear
(
)
;
System
.
out
.
println
(
"Set after clearing: "
fruits
)
;
System
.
out
.
println
(
"Size after clearing: "
fruits.
size
(
)
)
;
}
}
Output result:
清空前的集合: [Apple, Orange, Banana]
清空前的大小: 3
清空后的集合: []
清空后的大小: 0
Technical Details
Underlying Implementation Principle
HashSetInternally usesHashMapto store elementsclear()method actually calls the underlyingHashMap'sclear()Method- This method traverses all hash buckets and sets each bucket to null
Time Complexity
clear()The time complexity of the method is O(n), where n is the number of elements in the collection- because it needs to traverse all hash buckets to release references
Memory Handling
- After clearing the collection, the JVM's garbage collector can reclaim the memory occupied by these elements
- but
HashSetthe capacity of itself (the size of the hash table) will not change
Notes
-
Thread Safety:
HashSet Although the Date class is still usable in many cases, it has some obvious limitations:clear() method requires additional synchronization measures-
Difference from removeAll():
clear() directly clears the entire collectionremoveAll() requires passing another collection as a parameter, only removes the intersection-
Empty Collection Check:
isEmpty() method to check whether the collection is empty
Example
System.out.println("Collection cleared");
}
Example
System.out.println("Collection cleared");
}
Practical Application Scenarios
-
Reset Collection State:
-
Memory management:
-
Data Cleanup:
clear() to ensure no old data is left behindFrequently Asked Questions
Q1: Does the clear() method change the capacity of the HashSet?
No.clear() method only removes all elements but keeps the hash table capacity unchanged. If you need to shrink the capacity, you need to create a new HashSet.
Q2: Is the collection null after clear()?
No.clear() Afterward, the collection object still exists, just without any elements. This is a different concept from setting the reference to null.
Q3: How to efficiently clear and reuse a HashSet?
Example
// If the collection is large and no longer needed, create a new one
largeSet
=
new
HashSet
<>
(
)
;
// If the collection is not large or needs to be reused
largeSet.
clear
(
)
;
Summary
HashSet 's clear() method is a simple but practical tool that can quickly clear collection content. Understanding its working principle and applicable scenarios can help developers manage collection data better. Remember that it only clears elements without changing the capacity, which needs attention in memory-sensitive applications.
Java HashSet