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

Java HashSet removeAll() Method

Java HashSet removeAll() Method

Java HashSet Java HashSet


removeAll() method is the Java HashSet class, an important method used to remove all elements from the current collection that are the same as those in the specified collection. This method is inherited from java.util.AbstractSet class and is implemented in HashSet .

Method Syntax

public

boolean

removeAll

(

Collection

c

)

Method parameters

removeAll() method accepts one parameter:

Return Value

This method returns a boolean value:

Detailed Method Functionality

removeAll() The method performs the following operations:

  1. Traverse the parameter collection c all elements in the
  2. Check the current HashSet to see whether it contains these elements
  3. If they exist, remove them from the current HashSet remove these elements from
  4. As long as at least one element is removed, return true; otherwise it returns false

Usage examples

Basic Usage Example

Example

import

java.util.HashSet

;

public

class

RemoveAllExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create the first HashSet

        HashSet

<
String
set1
=
new
HashSet
<>
(
)

;

        set1.

add

(

"Apple"

)

;

        set1.

add

(

"Banana"

)

;

        set1.

add

(

"Orange"

)

;

        set1.

add

(

"Grape"

)

;

       

       

// Create the second HashSet

        HashSet

<
String
set2
=
new
HashSet
<>
(
)

;

        set2.

add

(

"Banana"

)

;

        set2.

add

(

"Grape"

)

;

        set2.

add

(

"Watermelon"

)

;

       

       

System

.

out

.

println

(

"set1 initial content: "

set1

)

;

       

System

.

out

.

println

(

"set2 content: "

set2

)

;

       

       

// Use the removeAll() method

       

boolean

isChanged

=

set1.

removeAll

(

set2

)

;

       

       

System

.

out

.

println

(

"set1 content after calling removeAll(): "

set1

)

;

       

System

.

out

.

println

(

"Was the collection modified: "

isChanged

)

;

   

}

}

Output result:

set1 初始内容: [Apple, Banana, Orange, Grape]
set2 内容: [Banana, Grape, Watermelon]
调用 removeAll() 后 set1 的内容: [Apple, Orange]
集合是否被修改: true

When no elements are removed

Example

HashSet

<
Integer
numbers
=
new
HashSet
<>
(
Arrays

.

asList

(

1

,

2

,

3

,

4

)

)

;

HashSet

<
Integer
toRemove
=
new
HashSet
<>
(
Arrays

.

asList

(

5

,

6

)

)

;

System

.

out

.

println

(

"Initial collection: "

numbers

)

;

boolean

result

=

numbers.

removeAll

(

toRemove

)

;

System

.

out

.

println

(

"After calling removeAll(): "

numbers

)

;

System

.

out

.

println

(

"Return value: "

result

)

;

Output result:

初始集合: [1, 2, 3, 4]
调用 removeAll() 后: [1, 2, 3, 4]
返回值: false

Notes

  1. Concurrent Modification: If you use while iterating over the collection removeAll() method, a may be thrown ConcurrentModificationException.

  2. Empty Collection Parameter: If the passed parameter is an empty collection, the method will return false, because no elements were removed.

  3. null Element:HashSet allows containing null elements,removeAll() can also handle correctly null values.

  4. Performance considerations: The time complexity of this method is O(n), where n is the size of the parameter collection. For large collections, this may affect performance.

  5. Modification Impact: Calling this method directly modifies the original collection rather than returning a new one.


Comparison with the retainAll() Method

removeAll() and retainAll() are two opposite operations:


Practical Application Scenarios

removeAll() method is very useful in the following scenarios:

  1. Filter out unwanted elements
  2. Compare the differences between two collections
  3. Implement the subtraction operation of collections
  4. Exclude a specific set of values during data cleaning

Summary

HashSet 's removeAll() method is a powerful collection operation tool that can efficiently remove elements from a collection in bulk. Understanding and correctly using this method can help developers handle collection data more effectively. Remember that it directly modifies the original collection, and use it reasonably based on actual needs.

Java HashSet Java HashSet