How to remove an element from ArrayList in Java?
Last Updated :
04 Jan, 2025
ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With the introduction and upgradations in java versions, newer methods are being available if we do see from Java8 perceptive lambda expressions and streams concepts were not available before it as it was introduced in java version8, so do we have more ways to operate over Arraylist to perform operations. Here we will be discussing a way to remove an element from an ArrayList.
Now, We will be discussing both ways via interpreting through a clean java program.
Methods:
There are 3 ways to remove an element from ArrayList as listed which later on will be revealed as follows:
- Using remove() method by indexes(default)
- Using remove() method by values
- Using remove() method over iterators
Note: It is not recommended to use ArrayList.remove() when iterating over elements.
Method 1: Using remove() method by indexes
It is a default method as soon as we do use any method over data structure it is basically operating over indexes only so whenever we do use remove() method we are basically removing elements from indices from an ArrayList.
ArrayList class provides two overloaded remove() methods.
Let us figure out with the help of examples been provided below as follows:
Example:
Java
// Java program to Remove Elements from ArrayList
// Using remove() method by indices
// Importing required classes
import java.util.ArrayList;
import java.util.List;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of List interface with
// reference to ArrayList class
List<Integer> al = new ArrayList<>();
// Adding elements to our ArrayList
// using add() method
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// Printing the current ArrayList
System.out.println(al);
// This makes a call to remove(int) and
// removes element 20
al.remove(1);
// Now element 30 is moved one position back
// So element 30 is removed this time
al.remove(1);
// Printing the updated ArrayList
System.out.println(al);
}
}
Output[10, 20, 30, 1, 2]
[10, 1, 2]
Now we have seen removing elements in an ArrayList via indexes above, now let us see that the passed parameter is considered an index. How to remove elements by value.
Method 2: Using remove() method by values
Example:
Java
// Java program to Remove Elements from ArrayList
// Using remove() method by values
// Importing required classes
import java.util.ArrayList;
import java.util.List;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of List interface with
// reference to ArrayList
List<Integer> al = new ArrayList<>();
// Adding elements to ArrayList class
// using add() method
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// Printing the current ArrayList
System.out.println(al);
// This makes a call to remove(Object) and
// removes element 1
al.remove(Integer.valueOf(1));
// This makes a call to remove(Object) and
// removes element 2
al.remove(Integer.valueOf(2));
// Printing the modified ArrayList
System.out.println(al);
}
}
Output :
[10, 20, 30,1 ,2]
[10, 20, 30]
Note: It is not recommended to use ArrayList.remove() when iterating over elements.
Also new Integer( int_value) has been deprecated since Java 9, so it is better idea to use Integer.valueOf(int_value) to convert a primitive integer to Integer Object.
Method 3: Using Iterator.remove() method
This may lead to ConcurrentModificationException When iterating over elements, it is recommended to use Iterator.remove() method.
Example:
Java
// Java program to demonstrate working of
// Iterator.remove() on an integer ArrayList
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList
List<Integer> al = new ArrayList<>();
// Adding elements to our ArrayList
// using add() method
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// Printing the current ArrayList
System.out.println(al);
// Creating iterator object
Iterator itr = al.iterator();
// Holds true till there is single element
// remaining in the object
while (itr.hasNext()) {
// Remove elements smaller than 10 using
// Iterator.remove()
int x = (Integer)itr.next();
if (x < 10)
itr.remove();
}
// Printing the updated ArrayList
System.out.print(al);
}
}
Output[10, 20, 30, 1, 2]
[10, 20, 30]
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read