remove(Object obj) method example Java ArrayList

In this tutorial, we will learn about the remove(Object obj) method which removes the specified object from the list. In the last tutorial I have shared how to remove element at the specified index in ArrayList using remove(int index) method.

Read Also: how ArrayList add(Object) method works internally in Java

According to Oracle docs, remove(Object obj) method removes the first occurrence of the specified element from the list, if it is present. Otherwise, if the specified element doesn't exist in the list, then the list is unchanged.

Syntax:


The syntax of the remove(Object obj) method of Java ArrayList is:

 public boolean remove(Object obj)

Parameters:


obj: The element needs to be removed from the list, if it is present.

Returns:


true, if the specified element is present in the list.

Java ArrayList remove(Object obj) example

1. Remove String object

In the below example, after adding elements to the ArrayList<String>, we are removing a few String elements from it.

 import java.util.*;
public class RemoveObjectMethodExample {
    public static void main(String args[]) {
        // Creating an object of ArrayList of String Type
        ArrayList<String> arrlist = new ArrayList();
        arrlist.add("Apple");
        arrlist.add("Google");
        arrlist.add("Meta");
        arrlist.add("Netflix");
        arrlist.add("Amazon");
        System.out.println("String ArrayList before remove method: ");
        for(String s : arrlist) {
            System.out.println(s);
        }
        // remove(obj) method returns true if the obj is present in the ArrayList
        boolean bool = arrlist.remove("Meta");
        System.out.println("Meta is removed: "+bool);
        // remove(obj) method returns false if the obj is not present in the ArrayList
        arrlist.remove("IBM");
        System.out.println("String ArrayList after applying remove method: ");
        for(String s2 : arrlist) {
            System.out.println(s2);
        }
    }
}


Output:
String ArrayList before remove method:
Apple
Google
Meta
Netflix
Amazon
Meta is removed: true
String ArrayList after applying remove method:
Apple
Google
Netflix
Amazon

2. Remove Integer object

In the below example, after adding elements to the ArrayList<Integer>, we are removing a few Integer elements from it.

 import java.util.*;
public class RemoveObjectMethodExample2 {
    public static void main(String args[]) {
        // Creating an object of ArrayList of Integer Type
        ArrayList<Integer> arrlist = new ArrayList();
        arrlist.add(new Integer(1111));
        arrlist.add(new Integer(2222));
        arrlist.add(new Integer(3333));
        arrlist.add(new Integer(4444));
        arrlist.add(new Integer(5555));
        System.out.println("Integer ArrayList before remove method: ");
        for(Integer i : arrlist) {
            System.out.println(i);
        }
        // remove(obj) method returns true if the obj is present in the ArrayList
        boolean bool = arrlist.remove(new Integer(2222));
        System.out.println("2222 is removed: " + bool);
        // remove(obj) method ) returns false if the obj is not present in the ArrayList
        arrlist.remove(new Integer(6666));
        System.out.println("Integer ArrayList after applying remove method: ");
        for(Integer i2 : arrlist) {
            System.out.println(i2);
        }
    }
}


Output:
Integer ArrayList before remove method:
1111
2222
3333
4444
5555
2222 is removed: true
Integer ArrayList after applying remove method:
1111
3333
4444
5555

That's all for today. Please mention in the comments in case you have any questions related to Java ArrayList remove(Object obj) method example.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry