Capacitor
0
Q:

ArrayList removeAll(Collection c) method in java

import java.util.ArrayList;
public class ArrayListRemoveIfMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>();
      al.add(15);
      al.add(8);
      al.add(58);
      al.add(19);
      // remove numbers divisible by 2
      al.removeIf(n -> (n % 2 == 0));
      // print list
      for(int a : al)
      {
         System.out.println(a);
      }
   }
}
2
import java.util.ArrayList;
public class ArrayListRemoveAllExample
{
   public static void main(String[] args)
   {
      try
      {
         ArrayList<Integer> al1 = new ArrayList<Integer>();
         al1.add(2);
         al1.add(4);
         al1.add(6);
         al1.add(8);
         al1.add(10);
         System.out.println("ArrayList before using removeAll() method: " + al1);
         // create another ArrayList
         ArrayList<Integer> al2 = new ArrayList<Integer>();
         al2.add(2);
         al2.add(4);
         al2.add(6);
         // print al2
         System.out.println("Elements to be removed: " + al2);
         // remove elements from ArrayList using removeAll() method
         al1.removeAll(al2);
         // print al1
         System.out.println("ArrayList after using removeAll() method: " + al1);
      }
      catch(NullPointerException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}
1

New to Communities?

Join the community