Java Program to Remove a Specific Element From a Collection
Last Updated :
10 Aug, 2021
remove() method is used to remove elements from a collection. It removes the element at the specified position in this list. Shifts any subsequent elements to the left by subtracts one from their indices. In simpler words, the remove() method is used for removing the element from a specific index from a list by removing the value and returning the same.
Approaches: There are two standard methods defined over this method which are as follows.
- remove(int index)
- remove(Object obj)
Method 1: The remove(int index) method of List interface in Java is used to remove an element from the specified index from a List container and returns the element after removing it. It also shifts the elements after the removed element by 1 position to the left in the List.
Syntax:
remove(int index)
Parameters: It takes one parameter of int type as traversing over-index where the index value is the value to be removed from the list.
Return Type: An ArrayList of which some elements have been deleted.
Exceptions: As dealing with indices with the size specified so definitely ArrayOutOfBounds is thrown in two scenarios
- Either index mention is negative
- Index mentioned is beyond(greater) the index of the ArrayList
Example:
Java
// Java Program to Remove a Specific
// Element from a Collection
// Importing Java libraries
import java.util.List;
import java.util.ArrayList;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList
List<Integer> al = new ArrayList<>();
// Inserting % elements to it
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// If elements set is larger use
// iteration in loops to insert element
// Display ArrayList after insertion
System.out.println("Original ArrayList : " + al);
// Making 1st remove call
// which throw 20 out of list
al.remove(1);
// Here ArrayList: 10 30 1 2
// Making 2nd remove call
// which throw 30 out of list
al.remove(1);
// Here ArrayList: 10 1 2
// Printing modified Arraylist after deleting few
// elements
System.out.println("Modified ArrayList : " + al);
}
}
OutputOriginal ArrayList : [10, 20, 30, 1, 2]
Modified ArrayList : [10, 1, 2]
Method 2: The remove(Object obj) method of List interface in Java is used to remove the first occurrence of the specified element obj from this List if it is present in the List.
Syntax:
boolean remove(Object obj)
Parameters: It accepts a single parameter obj of List type which represents the element to be removed from the given list.
Return Value: It returns a boolean value True after removing the first occurrence of the specified element from the List and otherwise if the element is not present in the List then this method will return False.
Example:
Java
// Java program to demonstrate working of remove()
// method on an integer arraylist
// Importing specific libraries
import java.util.List;
import java.util.ArrayList;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList and
// storing elements in list
List<Integer> al = new ArrayList<>();
// Addition of elements to List
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// This makes a call to remove(Object) and
// removes element 1
al.remove(new Integer(1));
// This makes a call to remove(Object) and
// removes element 2
al.remove(new Integer(2));
// Printing modified ArrayList
System.out.println("Modified ArrayList : " + al);
}
}
Output:
Modified ArrayList : [10, 20, 30]
Note: Sometimes it does throw a warning of using deprecated function call or object. One can recompile like to figure out where it is occurring. Generally, it is a bad idea to use deprecated libraries that may go away in the next release.
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