Java ArrayList removeAll() Examples
The removeAll() method is present in java.util.ArrayList class.
It is used to remove all the elements from the arrays List.
syntax
public boolean removeAll(Collection c)
c: a collection that contained elements to be removed from this list.
This method throws java.lang.NullPointerException: If the specified collection is null, or if the original list contains null elements and the specified collection does not permit null elements.
import java.util. * ;
public class ArrylistRemoveAll {
public static void main(String[] argv) throws Exception {
try {
ArrayList < String > arrlist1 = new ArrayList < String > ();
arrlist1.add("January");
arrlist1.add("February");
arrlist1.add("March");
arrlist1.add("April");
arrlist1.add("May");
System.out.println("ArrayList before " + "removeAll() operation : " + arrlist1);
ArrayList < String > arrlist2 = new ArrayList < String > ();
arrlist2.add("January");
arrlist2.add("February");
arrlist2.add("March");
arrlist2.add("April");
arrlist2.add("May");
arrlist2.add("June");
arrlist2.add("July");
arrlist2.add("August");
arrlist2.add("Septmeber");
arrlist2.add("October");
arrlist2.add("November");
arrlist2.add("December");
System.out.println("Collection in which the elements to be removed : ");
System.out.println(arrlist2);
arrlist2.removeAll(arrlist1);
System.out.println("ArrayList after using removeAll operation: ");
System.out.println(arrlist1);
}
catch(NullPointerException ex) {
System.out.println("Exception occured during removing elements from ArrayList: " + ex);
}
}
}
output:
ArrayList before removeAll() operation : [January, February, March, April, May]
Collection in which the elements to be removed :
[January, February, March, April, May, June, July, August, Septmeber, October, November, December]
ArrayList after using removeAll operation:
[January, February, March, April, May]
Remove null from the ArrayList using removeAll
When we try to remove the null collection from the ArrayList we will get NullPointerException.
import java.util.*; public class ArrylistRemoveAllNull { public static void main(String[] argv) throws Exception { try { ArrayList < String > arrlist1 = new ArrayList < String > (); arrlist1.add("January"); arrlist1.add("February"); arrlist1.add("March"); arrlist1.add("April"); arrlist1.add("May"); System.out.println("ArrayList before " + "removeAll() operation : " + arrlist1); ArrayList
arrlist2 = null; System.out.println("Collection in which the elements to be removed : " ); System.out.println(arrlist2); arrlist2.removeAll(arrlist1); System.out.println("ArrayList after using removeAll operation: " ); System.out.println(arrlist1); } catch (NullPointerException ex) { System.out.println("Exception occured during removing elements from ArrayList: " + ex); } } } output: ArrayList before removeAll() operation : [January, February, March, April, May] Collection in which the elements to be removed : null Exception occured during removing elements from ArrayList: java.lang.NullPointerException
Post/Questions related to Java BufferedWriter
How to find the sum of elements in ArrayList in java?
How to display objects in ArrayList in java?
How to search in ArrayList in java?
How to remove all elements from ArrayList in java?
How to add elements in ArrayList in java?
How do you remove the last element of an ArrayList in Java?
How to empty an ArrayList in java?
How do you remove null elements from an ArrayList in Java?
How to remove multiple elements from ArrayList in java?
In this article, we have seen Java ArrayList removeAll() Examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment