how To Remove element from Arraylist In java

There is two way to remove element from arraylist .

  1. Removing element using ArrayList.remove() method
  2. Removing element using ArrayList. removeIf(Predicate p) method
  3. Removing element using iterator’s remove method.

1. Removing element using ArrayList.remove() method

Arraylist provide two overloaded  remove() method .

a. E remove(int index) – It removes the element at the given position in this list.

b. boolean remove(Object o) – It removes the first occurrence of the given element from this list, if it is present.

Let’s remove(int index) take first

Syntax :

public E remove(int index) {

}
  • It removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
  • This Method Take index of the element to be removed as parameter
  • It returns: the element that was removed from the list
  • It throws IndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size())
import java.util.ArrayList;

public class ArrayListRemoveElement {
	
	public static void main(String[] args) {
				
		ArrayList<String> arr = new ArrayList<String>();

		// add element in arraylist
		arr.add("c");
		arr.add("php");
		arr.add("html");
		arr.add("java");

		// print arraylist
		System.out.println("Before Remove Element ArrayList is = " + arr);

		/*
		* Removes the element at the specified position in this
		list. Shifts any subsequent elements to the left
		*/
		
		String str =arr.remove(1);
		System.out.println("Remove Element from ArrayList is="+str);
		System.out.println("After Remove Element ArrayList is="+arr);
		
		
	}
	

}

Output :

Before Remove Element ArrayList is = [c, php, html, java]
Remove Element from ArrayList is= php
After Remove Element ArrayList is=[c, html, java]

Now we discuss boolean remove(Object o) method

Syntax :

public boolean remove(Object o) {

}
  • It removes the first occurrence of the specified element from this list, if it is present.
  • If the list does not contain the element, it is unchanged.
  • It Take o element to be removed from this list as parameter ,
  • it returns true if this list contained the given element
import java.util.ArrayList;

public class ArrayListRemoveElement {
	
	public static void main(String[] args) {
				
		ArrayList<String> arr = new ArrayList<String>();

		// add element in arraylist
		arr.add("c");
		arr.add("php");
		arr.add("html");
		arr.add("java");

		// print arraylist
		System.out.println("Before Remove Element ArrayList is = " + arr);

		/*
		 * It removes the first occurrence of the specified
		 *  element from this list, if it is present.		
		 */
		arr.remove("php");
		
		//print list
		System.out.println("After Remove Element ArrayList is="+arr);
		
		
	}
	

}

Output :

Before Remove Element ArrayList is = [c, php, html, java]
After Remove Element ArrayList is=[c, html, java]

2. Removing element using ArrayList. removeIf(Predicate p) method

It removes all of the elements of this collection that satisfy the given predicate. It means with the help of this method we can  remove multiple elements from arraylist in java.

import java.util.ArrayList;

public class ArrayListRemoveElement {
	
	public static void main(String[] args) {
				
		ArrayList<String> arr = new ArrayList<String>();

		// add element in arraylist
		arr.add("c");
		arr.add("php");
		arr.add("html");
		arr.add("java");

		// print arraylist
		System.out.println("Before Remove Element ArrayList is = " + arr);

		arr.removeIf( c -> c.equals("php"));
		
		//print list
		System.out.println("After Remove Element ArrayList is="+arr);
		
		
	}
	

}

Output :

Before Remove Element ArrayList is = [c, php, html, java]
After Remove Element ArrayList is=[c, html, java]

3. Removing element using iterator’s remove method

If you want to remove an element from ArrayList while iterating the ArrayList then you should use remove() method of the Iterator itself. It is required because it is not permitted to structurally modify (add or remove elements) ArrayList at any time after the iterator is created except through the iterator’s own remove method not doing so will result in ConcurrentModificationException being thrown. Let’s see this by code

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListRemoveElement {
	
	public static void main(String[] args) {
				
		ArrayList<String> arr = new ArrayList<String>();

		// add element in arraylist
		arr.add("c");
		arr.add("php");
		arr.add("html");
		arr.add("java");

		// print arraylist
		//System.out.println("Before Remove Element ArrayList is = " + arr);

		Iterator<String> itr = arr.iterator();
	    while(itr.hasNext()){
	      String str = itr.next();
	      if(str.equals("php")) {
	        // Using ArrayList's remove method
	    	  arr.remove(str);
	      }
	    }
	    
	    
		
		//print list
		System.out.println("After Remove Element ArrayList is="+arr);
		
		
	}
	

}

Output :

Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at com.demo.arraylist.ArrayListRemoveElement.main(ArrayListRemoveElement.java:23)

If we remove element by using iterator remove method then it will works as

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListRemoveElement {
	
	public static void main(String[] args) {
				
		ArrayList<String> arr = new ArrayList<String>();

		// add element in arraylist
		arr.add("c");
		arr.add("php");
		arr.add("html");
		arr.add("java");

		// print arraylist
		System.out.println("Before Remove Element ArrayList is = " + arr);

		Iterator<String> itr = arr.iterator();
	    while(itr.hasNext()){
	      String str = itr.next();
	      if(str.equals("php")) {
	        // Using Iterator's remove method
	    	  itr.remove();
	      }
	    }
	    
	    
		
		//print list
		System.out.println("After Remove Element ArrayList is="+arr);
		
		
	}
	

}

Output:

Before Remove Element ArrayList is = [c, php, html, java]
After Remove Element ArrayList is=[c, html, java]

 

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 64 = 66