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

Java HashSet remove() Method

Java HashSet remove() Method

Java HashSet Java HashSet


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

- can be any type of object - If the element does not exist in the collection, no operation is performed

Return Value

- `true`: If the collection contains the specified element and successfully removes it - `false`: 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:


Summary

HashSet.remove() method is the operation for HashSet collection, and it provides efficient element removal capability. When using it, note:

  1. Correctly implement equals() and hashCode() method (for custom objects)
  2. Handle concurrent modification issues
  3. 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 Java HashSet