CopyOnWriteArrayList remove() method in Java with Examples - GeeksforGeeks

06 Jan, 2020
The remove()method of CopyOnArrayList in Javais used to remove the element in the list.
Syntax:
1. public E remove(int index)
2. public boolean remove(Object o)

The remove(int index) method of CopyOnArrayList in Java is used to remove the element at the specified position in the list.
Syntax:
public E remove(int index)

Parameters: This method accepts a mandatory parameter index which specifies the position of the element.
Return Type: This method returns the list after deleting the specified element.
Exception: This method throws ArrayIndexOutOfBounds exception if specified index is out of range i.e index is less than 0 or greater than or equal to the size of the list.
Below programs illustrate the remove(int index) method of CopyOnArrayList in Java:
Program 1: This program involves CopyOnArrayList remove(int index) of Integer type
// Java Program to illustrate CopyOnArrayList// remove(int index) method  import java.util.concurrent.CopyOnWriteArrayList;import java.util.*;  public class GFG {    public static void main(String[] args)    {        CopyOnWriteArrayList<Integer> ArrLis1            = new CopyOnWriteArrayList<Integer>();          // Add elements        ArrLis1.add(63);        ArrLis1.add(54);        ArrLis1.add(81);        ArrLis1.add(96);          // print CopyOnWriteArrayList        System.out.println("CopyOnWriteArrayList: "                           + ArrLis1);          ArrLis1.remove(2);        // check using function        System.out.println(ArrLis1);    }}
CopyOnWriteArrayList: [63, 54, 81, 96]
[63, 54, 96]

Program 2: This program involves CopyOnArrayList remove(int index) of String type
// Java Program to illustrate CopyOnArrayList// remove(int index) method  import java.util.concurrent.CopyOnWriteArrayList;import java.util.*;  public class GFG {    public static void main(String[] args)    {        CopyOnWriteArrayList<String> ArrLis1            = new CopyOnWriteArrayList<String>();          // Add elements        ArrLis1.add("geeks");        ArrLis1.add("gfg");        ArrLis1.add("programming");          // print CopyOnWriteArrayList        System.out.println("CopyOnWriteArrayList: "                           + ArrLis1);          ArrLis1.remove(0);        // check using function        System.out.println(ArrLis1);    }}
CopyOnWriteArrayList: [geeks, gfg, programming]
[gfg, programming]

The remove(Object o) method of CopyOnArrayList in Java is used to removes the first occurrence of specified element, if it is present in the list.
Syntax:
public boolean remove(Object o)

Parameters: This method accepts a mandatory parameter o, the element which is to be removed from the list, if present.
Return Type: This method returns true if specified element is present in the list, else false.
Below programs illustrate the remove(Object o) method of CopyOnArrayList in Java:
Program 1: This program involves CopyOnArrayList remove(Object o) of Integer type
// Java Program to illustrate CopyOnArrayList// remove(Object o) method  import java.util.concurrent.CopyOnWriteArrayList;import java.util.*;  public class GFG {    public static void main(String[] args)    {        CopyOnWriteArrayList<Integer> ArrLis1            = new CopyOnWriteArrayList<Integer>();          // Add elements        ArrLis1.add(11);        ArrLis1.add(22);        ArrLis1.add(33);        ArrLis1.add(22);        ArrLis1.add(44);          // print CopyOnWriteArrayList        System.out.println("CopyOnWriteArrayList: "                           + ArrLis1);          ArrLis1.remove(new Integer(22));        // check using function        System.out.println(ArrLis1);    }}
CopyOnWriteArrayList: [11, 22, 33, 22, 44]
[11, 33, 22, 44]

Program 2: This program involves CopyOnArrayList remove(Object o) of String type
// Java Program to illustrate CopyOnArrayList// remove(Object o) method  import java.util.concurrent.CopyOnWriteArrayList;import java.util.*;  public class GFG {    public static void main(String[] args)    {        CopyOnWriteArrayList<String> ArrLis1            = new CopyOnWriteArrayList<String>();          // Add elements        ArrLis1.add("geeks");        ArrLis1.add("gfg");        ArrLis1.add("programming");          // print CopyOnWriteArrayList        System.out.println("CopyOnWriteArrayList: "                           + ArrLis1);          ArrLis1.remove("gfg");        // check using function        System.out.println(ArrLis1);    }}
CopyOnWriteArrayList: [geeks, gfg, programming]
[geeks, programming]

nidhi_biet
Java-CopyOnWriteArrayList
Java-Functions
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Multidimensional Arrays in Java
Singleton Class in Java
LinkedList in Java
Collections in Java