BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList remove(Object obj) Method example

Last Updated: June 1, 2024 by Chaitanya Singh | Filed Under: java

In this guide, you will learn how to remove a specified element from an ArrayList using remove() method. The method remove(Object obj) removes the first occurrence of the specified object(element) from the list. It belongs to the java.util.ArrayList class.

public boolean remove(Object obj)

Note:

  • It returns false if the specified element doesn’t exist in the list.
  • If there are duplicate elements present in the list it removes the first occurrence of the specified element from the list.

Java Program to demonstrate ArrayList.remove(Object obj) method

In this example, we have an ArrayList of strings. We can remove a specific string from the list using remove() method.

import java.util.ArrayList;
public class RemoveExample {
public static void main(String args[]) {
//String ArrayList
ArrayList<String> al = new ArrayList<>();
al.add("AA");
al.add("BB");
al.add("CC");
al.add("DD");
al.add("EE");
al.add("FF");

// Displaying arraylist before removing any element
System.out.println("ArrayList before remove:");
for(String var: al){
System.out.println(var);
}

//Removing element AA from the arraylist
al.remove("AA");
//Removing element FF from the arraylist
al.remove("FF");
//Removing element CC from the arraylist
al.remove("CC");

// The element "GG" doesn't exit in the list so this
// method returns false, which is stored in variable b
boolean b = al.remove("GG");
System.out.println("Element GG removed: "+b);

// Displaying arraylist after all the remove operations
System.out.println("ArrayList After remove:");
for(String var2: al){
System.out.println(var2);
}
}
}

Output:

ArrayList before remove:
AA
BB
CC
DD
EE
FF
Element GG removed: false
ArrayList After remove:
BB
DD
EE
❮ Java ArrayListJava Collections ❯

Top Related Articles:

  1. How to get the last element of Arraylist?
  2. How to loop HashMap in java
  3. How to Convert an array to ArrayList in java
  4. ArrayList in Java With Examples
  5. Multilevel inheritance in java with example

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Troy Taylor says

    October 17, 2015 at 1:54 AM

    Very clear, very helpful. Thanks ++

    Reply
  2. Deepika D says

    October 21, 2018 at 5:41 PM

    I have a question : if ArrayList list…. is defined and list.remove(Integer) is called.
    will it take that Integer parameter as index or as a element of the list ????

    Reply
    • Chaitanya Singh says

      October 29, 2018 at 1:57 PM

      Lets say we have an arraylist of type integer then using list.remove(1) will remove the element at the position 1. However if you want to remove the element that has value 1 then do it like this: list.remove(Integer.valueOf(1)).

      Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap