- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove all the elements from an ArrayList that are in another Collection in Java
The method java.util.ArrayList.removeAll() removes all the the elements from the ArrayList that are available in another collection. This method has a single parameter i.e. the Collection whose elements are to be removed from the ArrayList.
A program that demonstrates this is given as follows
Example
import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List aList1 = new ArrayList(); aList1.add("Anna"); aList1.add("John"); aList1.add("Mary"); aList1.add("Amy"); aList1.add("Harry"); List aList2 = new ArrayList(); aList2.add("John"); aList2.add("Harry"); aList2.add("Amy"); aList1.removeAll(aList2); System.out.println("The elements in ArrayList aList1 are: " + aList1); } }
The output of the above program is as follows
The elements in ArrayList aList1 are: [Anna, Mary]
Now let us understand the above program.
The ArrayLists aList1 and aList2 are created. Then ArrayList.add() is used to add the elements to both the ArrayList. A code snippet which demonstrates this is as follows
List aList1 = new ArrayList(); aList1.add("Anna"); aList1.add("John"); aList1.add("Mary"); aList1.add("Amy"); aList1.add("Harry"); List aList2 = new ArrayList(); aList2.add("John"); aList2.add("Harry"); aList2.add("Amy");
The ArrayList.removeAll() method is used to remove all the the elements from the aList1 that are available in aList2. Then the elements in aList1 are printed. A code snippet which demonstrates this is as follows
aList1.removeAll(aList2); System.out.println("The elements in ArrayList aList1 are: " + aList1);
- Related Articles
- Remove all elements from the ArrayList in Java
- How to remove all elements from a Collection in another Collection
- Remove all elements from the ArrayList in C#
- Remove all elements from the Collection in C#
- Create a new ArrayList from another collection in Java
- Append all elements of other Collection to ArrayList in Java
- How to remove the redundant elements from an ArrayList object in java?
- How to remove all elements of ArrayList in Java?
- Remove all elements in a collection from a HashSet in C#
- Remove duplicate items from an ArrayList in Java
- Java Program to Remove duplicate elements from ArrayList
- Append all elements of another Collection to a Vector in Java
- Remove all elements from TreeSet in Java
- Remove an element from an ArrayList using the ListIterator in Java
- Java Program to append all elements of other Collection to ArrayList
