beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java ArrayList remove(Object obj) Method example

By Chaitanya Singh | Filed Under: Java Collections

The method remove(Object obj) removes the specified object 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.

Example

In this example we have an ArrayList<String> and we are removing few strings from the list.

package beginnersbook.com;
import java.util.ArrayList;
public class RemoveExample {
   public static void main(String args[]) {
       //String ArrayList
       ArrayList<String> al = new ArrayList<String>();
       al.add("AA");
       al.add("BB");
       al.add("CC");
       al.add("DD");
       al.add("EE");
       al.add("FF");
       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");
       /*This element is not present in the list so
        * it should return false
        */
       boolean b=al.remove("GG");
       System.out.println("Element GG removed: "+b);
       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

Enjoyed this post? Try these related posts

  1. Comparator Interface in Java
  2. Remove mapping from Hashtable example – Java
  3. Java ArrayList set() Method example
  4. Java ArrayList trimToSize() Method example
  5. Java – Get element from specific index of LinkedList example
  6. Java ArrayList isEmpty() Method example

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 *

ArrayList Tutorial

  • Java ArrayList

Java ArrayList Methods

  • add(Object obj)
  • add(int index, Object element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • contains()
  • get()
  • indexOf()
  • ensureCapacity()
  • isEmpty()
  • lastIndexOf()
  • remove()
  • remove(Object obj)
  • trimToSize()
  • set()
  • clone()
  • clear()
  • size()

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap