Remove an Element from ArrayList in Java

ArrayList is similar to the array whose size can be modified. The Array List class is available in the Java.util package and extends the List interface. Adding and removing an element from the Array List is very easy by using its built-in methods add() and remove(). However, there is more than one way of removing an element from the Array List that are as follows:

  1. Using ArrayList.remove() Method
    1. By index.
    2. By element
  2. Using Iterator.remove() Method
  3. Using ArrayList.removeIf() Method

All these three ways are best in their own, and can be used in some different scenario. Let’s understand all these three ways, one by one.

ArrayList.remove() Method: Using the remove() method of the Array List class is the fastest way of deleting or removing the element from the Array List. It also provides the two overloaded methods, i.e., remove(int index) and remove(Object obj). The remove(int index) method accepts the index of the object to be removed, and the remove(Object obj) method accepts the object to be removed.

Let’s take an example to understand how the remove() method is used. RemoveMethod.java

Output
    import java.util.ArrayList;  
      
    public class RemoveMethod {  
       public static void main(String[] args) {  
          // creating an ArrayList having default size 5  
          ArrayList<String> arr = new ArrayList<String>(5);  
          // Adding elements to the ArrayList  
          arr.add("Helen");  
          arr.add("Paul");  
          arr.add("Elanie");  
          arr.add("Marco");  
          System.out.println("The list of the size is: " + arr.size());  
          // Showing all the elements in the ArrayList  
          for (String name : arr) {  
             System.out.println("Name is: " + name);  
          }    
          // Removing element available at position 1  
          arr.remove(1);  
          System.out.println("\nAfter removing the element the size of the ArrayList is: " + arr.size());  
          // Showing all the elements in the ArrayList  
          for (String name : arr) {  
             System.out.println("Name is: " + name);  
          }  
       }  
    }  

Let’s take another example to understand how the remove() method is used to remove the specified element from the Array List. RemoveElementMethod.java

Output
    import java.util.ArrayList;  
      
    public class RemoveElementMethod {  
       public static void main(String[] args) {  
            
          // creating an ArrayList having default size 5  
          ArrayList<String> arr = new ArrayList<String>(5);  
      
          // Adding elements to the ArrayList  
          arr.add("Helen");  
          arr.add("Paul");  
          arr.add("Elanie");  
          arr.add("Marco");  
      
          System.out.println("The list of the size is: " + arr.size());  
            
          // Showing all the elements in the ArrayList  
          for (String name : arr) {  
             System.out.println("Name is: " + name);  
          }    
            
          // Removing the specified element from ArrayList  
          arr.remove("Paul");  
      
          System.out.println("\nAfter removing the element the size of the ArrayList is: " + arr.size());  
            
          // Showing all the elements in the ArrayList  
          for (String name : arr) {  
             System.out.println("Name is: " + name);  
          }  
       }  
    }  

Iterator.remove() Method: The Iterator.remove() method is another way of removing an element from an Array List. It is not so helpful in case when iterating over elements. When we use the remove() method while iterating the elements, it throws the ConcurrentModificationException. The Iterator class removes elements properly while iterating the Array List.

Let’s take an example to understand how the Iterator.remove() method is used. IteratorRemoveMethod.java

Output
    import java.util.ArrayList;  
    import java.util.Iterator;   
      
    public class iteratorRemoveMethod {  
        public static void main(String[] args) {  
            
            // creating an ArrayList having default size 10  
            ArrayList<Integer> numbers = new ArrayList<Integer>(10);  
      
            // Adding elements to the ArrayList   
            numbers.add(12);  
            numbers.add(1);  
            numbers.add(8);  
            numbers.add(5);  
            numbers.add(9);  
            System.out.println("The list of the size is: " + numbers.size());  
            // Showing all the elements in the ArrayList  
            for (Integer number : numbers) {  
                System.out.println("Number is: " + number);  
            }    
            // Removing elements greater than 10 using remove() method  
            Iterator itr = numbers.iterator();   
            while (itr.hasNext())   
            {   
                int data = (Integer)itr.next();   
                if (data > 10)   
                    itr.remove();   
            }   
      
            System.out.println("\nAfter removing the element the size of the ArrayList is: " + numbers.size());  
        
            // Showing all the elements in the ArrayList  
            for (Integer number : numbers) {  
                System.out.println("Number is: " + number);  
            }  
        }  
    }  

ArrayList.removeIf() Method: If we want to remove an element from the ArrayList which satisfy the predicate filter, the removeIf() method is best suited for this case. We pass the predicate filter to that method as an argument.

Let’s take an example to understand how the removeIf() method is used. RemoveIfMethod.java

Output
    import java.util.ArrayList;  
    public class RemoveIfMethod {  
        public static void main(String[] args) {        
            // creating an ArrayList having default size 10  
            ArrayList<String> cities = new ArrayList<String>(10);  
            // Adding elements to the ArrayList   
            cities.add("Berlin");  
            cities.add("Bilbao");  
            cities.add("Cape Town");  
            cities.add("Nazilli");  
            cities.add("Uribia");  
            cities.add("Gliwice");  
            System.out.println("The list of the size is: " + cities.size());  
            // Showing all the elements in the ArrayList  
            for (String city : cities) {  
                System.out.println("City is: " + city);  
            }    
            // Removing elements which are start with B using removeIf() method  
             cities.removeIf(n -> (n.charAt(0) == 'B'));  
            System.out.println("\nAfter removing the element the size of the ArrayList is: " + cities.size());  
            // Showing all the elements in the ArrayList  
            for (String city : cities) {  
                System.out.println("City is: " + city);  
            }  
        }  
    }  

All of the above-discussed methods are used for different scenarios. Using ArrayList.remove() method is the fastest way for removing an element from the Array List.

www.000webhost.com