
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I remove multiple elements from a list in Java?
A List extends a collection that is used to store elements in a sequential manner. Let's learn how to remove multiple elements from a list in Java. The following are a few ways to do that:
Using a For Loop
To remove multiple elements from a list using a for loop. We will iterate through the list and check if each element is in the list of elements to be removed. If it is, we will remove it from the original list.
Example
Following is an example of removing multiple elements from a list using a for loop:
import java.util.ArrayList; import java.util.List; public class RemoveMultipleElements { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println("Original List: " + list); List<Integer> elementsToRemove = new ArrayList<>(); elementsToRemove.add(2); elementsToRemove.add(4); for (int i = 0; i < list.size(); i++) { if (elementsToRemove.contains(list.get(i))) { list.remove(i); i--; } } System.out.println("List after removal: " + list); } }
Output
Following is the output of the above code:
Original List: [1, 2, 3, 4, 5] List after removal: [1, 3, 5]
Using removeAll() Method
The removeAll() method is a built-in method of the List interface in Java. It is used to remove all elements from a list to another collection. The filter() method is used to eliminate elements based on a criterion.
Example
In the below example, we will create a list of integers and then use the removeAll() method to remove multiple elements from that list.
import java.util.ArrayList; import java.util.List; public class RemoveMultipleElements { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println("Original List: " + list); List<Integer> elementsToRemove = new ArrayList<>(); elementsToRemove.add(2); elementsToRemove.add(4); list.removeAll(elementsToRemove); System.out.println("List after removal: " + list); } }
Output
Following is the output of the above code:
Original List: [1, 2, 3, 4, 5] List after removal: [1, 3, 5]
Using Stream API
Now, let's use the Stream API to remove multiple elements from a list. The filter() method will filter out the elements that we want to keep and then collect the results into a new list.
Example
Following is an example of removing multiple elements from a list using the Stream API:
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class RemoveMultipleElements { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(12); list.add(42); list.add(34); list.add(41); list.add(25); System.out.println("Original List: " + list); List<Integer> elementsToRemove = new ArrayList<>(); elementsToRemove.add(25); elementsToRemove.add(41); List<Integer> filteredList = list.stream() .filter(e -> !elementsToRemove.contains(e)) .collect(Collectors.toList()); System.out.println("List after removal: " + filteredList); } }
Output
Following is the output of the above code:
Original List: [12, 42, 34, 41, 25] List after removal: [12, 42, 34]