Java HashSet remove() Method
HashSet.remove() is in the Java Collections Framework HashSet class, an important method provided, used to remove the specified element from the collection. This method is inherited from java.util.AbstractCollection class and is implemented in HashSet where the concrete functionality is implemented.
Method Syntax
Example
public
boolean
remove
(
Object
o
)
Method parameters
Object o: The element to remove from the collection- can be any type of object
- If the element does not exist in the collection, no operation is performed
Return Value
- Return
booleanType: true: If the collection contains the specified element and successfully removes itfalse: If the collection does not contain the specified element
Method Characteristics
Hash Table Based Implementation
HashSet Internally uses a hash table to store elements,remove() method quickly locates and removes the element by computing its hash code, with an average time complexity of O(1).
Element Uniqueness
HashSet does not allow duplicate elements, so remove() method only removes the first matching element (if there are multiple identical elements, in fact HashSet does not store duplicate elements).
Compatibility with null
HashSet allows storing null values, so you can also use remove() method to remove null .
Usage examples
Basic Usage
Example
import
java.util.HashSet
;
public
class
HashSetRemoveExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a HashSet
HashSet
<
String
fruits
=
new
HashSet
<>
(
);
// Add elements
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Orange"
)
;
fruits.
add
(
null
)
;
// Add a null element
System
.
out
.
println
(
"Original collection: "
fruits
)
;
// Output: Original collection: [null, Apple, Orange, Banana]
// Remove element
boolean
isRemoved
=
fruits.
remove
(
"Banana"
)
;
System
.
out
.
println
(
"Remove 'Banana': "
isRemoved
)
;
// Output: Remove 'Banana': true
// Try to remove a non-existent element
isRemoved
=
fruits.
remove
(
"Grape"
)
;
System
.
out
.
println
(
"Remove 'Grape': "
isRemoved
)
;
// Output: Remove 'Grape': false
// Remove null element
isRemoved
=
fruits.
remove
(
null
)
;
System
.
out
.
println
(
"Remove null: "
isRemoved
)
;
// Output: Remove null: true
System
.
out
.
println
(
"Modified collection: "
fruits
)
;
// Output: Modified collection: [Apple, Orange]
}
}
Using Custom Objects
When using custom objects, you need to correctly override equals() and hashCode() method, otherwise remove() method may not work properly:
Example
import
java.util.HashSet
;
class
Student
{
private
int
id
;
private
String
name
;
public
Student
(
int
id,
String
name
)
{
this
.
id
=
id
;
this
.
name
=
name
;
}
// Must override equals and hashCode methods
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
(
)
!=
o.
getClass
(
)
)
return
false
;
Student student
=
(
Student
)
o
;
return
id
==
student.
id
&&
name.
equals
(
student.
name
)
;
}
@Override
public
int
hashCode
(
)
{
return
31
id
name.
hashCode
(
)
;
}
@Override
public
String
toString
(
)
{
return
"Student{"
"id="
id
", name='"
name
'''
'}'
;
}
}
public
class
CustomObjectExample
{
public
static
void
main
(
String
[
]
args
)
{
HashSet
<
Student
students
=
new
HashSet
<>
(
);
students.
add
(
new
Student
(
1
,
"Alice"
)
)
;
students.
add
(
new
Student
(
2
,
"Bob"
)
)
;
System
.
out
.
println
(
"Original collection: "
students
)
;
// Try to remove a student
boolean
removed
=
students.
remove
(
new
Student
(
1
,
"Alice"
)
)
;
System
.
out
.
println
(
"Remove Alice: "
removed
)
;
// Output: Remove Alice: true
System
.
out
.
println
(
"Modified collection: "
students
)
;
}
}
Notes
Concurrent Modification
HashSet is not thread-safe. If you try to call while iterating over the collection (for example, using a for-each loop) remove() method, a will be thrown ConcurrentModificationException. If you need to remove elements while iterating, you should use the iterator's remove() method.
Performance considerations
Although HashSet.remove() has an average time complexity of O(1), but in severe hash collision scenarios, performance degrades to O(n). Therefore, a good hashCode() implementation is important for maintaining high performance.
Difference from Iterator's remove()
Directly calling HashSet.remove() and calling via the iterator remove() differ as follows:
HashSet.remove(): Removes based on element valueIterator.remove(): Removes the element currently being iterated
Summary
HashSet.remove() method is the operation for HashSet collection, and it provides efficient element removal capability. When using it, note:
- Correctly implement
equals()andhashCode()method (for custom objects) - Handle concurrent modification issues
- Understand what the return value indicates about the operation result
By using properly remove() method, you can effectively manage HashSet elements in the collection.
Java HashSet