Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Term Entry] Java ArrayList .removeAll() #4581

Merged
merged 11 commits into from
May 21, 2024
67 changes: 67 additions & 0 deletions content/java/concepts/array-list/terms/removeAll/removeAll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
Title: '.removeAll()'
Description: 'Removes multiple elements from an ArrayList.'
avdhoottt marked this conversation as resolved.
Show resolved Hide resolved
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Arrays'
- 'Data Types'
bpicoCode marked this conversation as resolved.
Show resolved Hide resolved
- 'Java'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

The **`.removeAll()`** method is used for removing multiple elements from instances of the `ArrayList` class.

## Syntax

A collection of elements, specified by `Collection c`, can be removed from an `ArrayList` instance using the `.removeAll()` method:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line and add the description of the parameters ArrayList2 and ArrayList1 below the syntax in this format -

- `parameterName`: Parameter description.
- `parameterName`: Parameter description.


```pseudo
arrayListInstance.removeAll(Collection c);
avdhoottt marked this conversation as resolved.
Show resolved Hide resolved
```

## Example

In the example below, two `ArrayList` instances, `fallMonths` and `monthsToRemove`, are created to hold `String`-type elements. Elements are added to the `fallMonths` using the [`.add()`] method (https://www.codecademy.com/resources/docs/java/array-list/add). Then, `monthsToRemove` is utilized to remove the months - August and September from `fallMonths` using `.removeAll()`. Finally, all elements are removed from `fallMonths` by invoking `.removeAll()` with a reference to itself.
bpicoCode marked this conversation as resolved.
Show resolved Hide resolved

```java
import java.util.ArrayList;

public class RemoveFallMonths{
public static void main(String[] args) {

ArrayList<String> fallMonths = new ArrayList<String>();

fallMonths.add("August");
fallMonths.add("September");
fallMonths.add("October");
fallMonths.add("November");
fallMonths.add("December");

ArrayList<String> monthsToRemove = new ArrayList<String>();

monthsToRemove.add("August");
monthsToRemove.add("December");

// Remove August and December from fallMonths using monthsToRemove
fallMonths.removeAll(monthsToRemove);

System.out.println("After removing specified months: " + fallMonths);```

// Remove all elements from fallMonths
fallMonths.removeAll(fallMonths);

System.out.println("After removing all elements: " + fallMonths);```
}
}
avdhoottt marked this conversation as resolved.
Show resolved Hide resolved
```

The `RemoveFallMonths` output should look like this:
avdhoottt marked this conversation as resolved.
Show resolved Hide resolved

```shell
[September, October, November]
[]
avdhoottt marked this conversation as resolved.
Show resolved Hide resolved
```