Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Removing Element from the Specified Index in Java ArrayList
Next article icon

Java Program to Remove a Specific Element From a Collection

Last Updated : 10 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  1. remove(int index)
  2. 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);
    }
}
 
 

 
 

Output
Original 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.

 



Next Article
Removing Element from the Specified Index in Java ArrayList
author
aditya_taparia
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Collections
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Java Program to Remove Duplicate Elements From the Array
    Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted. Example: [GFGTABS] Java // Java Program to Remove Dup
    6 min read
  • 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 Replace an Element in a List
    List in Java provides a way to store the ordered collection. The list Interface provides various methods to add, delete, or replace items in the List. In this article, we will learn about how we could replace an element in the list in Java. Methods to Replace an Element in a ListThere are 3 ways to
    4 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
  • Removing Element from the Specified Index in Java ArrayList
    The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices). Syntax : public removed_element remove(int index) Parameters: The index of the element
    3 min read
  • Java Program to Remove Duplicate Entries from an Array using TreeSet
    Features of TreeSet is the primary concern it is widely used in remove duplicates in the data structure as follows: TreeSet implements the SortedSet interface. So, duplicate values are not allowed and will be leftovers.Objects in a TreeSet are stored in a sorted and ascending order.TreeSet does not
    3 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
  • How to Insert all the Collection Elements to the Specified Position in Java ArrayList?
    The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this meth
    3 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 Change a Collection to an Array
    An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences