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 Program To Remove All The Duplicate Entries From The Collection As we know that the HashSet contains only unique elements, ie no duplicate entries are allowed, and since our aim is to remove the duplicate entries from the collection, so for removing all the duplicate entries from the collection, we will use HashSet.The HashSet class implements the Set interface,
3 min read
Java Program to Remove an Element from ArrayList using ListIterator ListIterator.remove() method removes the last element from the list that was returned by next() or previous() cursor positions. It can be called only once per call to next or previous. It can be made only if the operation â add(E) has not called after the last call to next or previous. Internal work
4 min read
Java Collection Programs - Basic to Advanced As it cleared from its name itself "Collection" it is a pre-defined collective bunch of classes and Interfaces present in the "Collection Framework" in Java. Their Classes, Interfaces and Methods are frequently used in competitive programming. This article provides a variety of programs on Java Coll
4 min read
Removing last element from ArrayList in Java Given an ArrayList collection in Java, the task is to remove the last element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 1, 2] Output: [10, 20, 30, 1] After removing the last element 2, the ArrayList is: [10, 20, 30, 1] Input: ArrayList[] = [1, 1, 2, 2, 3] Output: [1, 1, 2, 2] Af
2 min read
Java Program to Find the Intersection Between Two Collection Collection means a set of different classes and interfaces are group together into a single unit that has similar functions are called collection and framework we know very that provides a predefined architecture to represents and manipulate collections in java. Here we will be discussing discuss ou
4 min read