CopyOnWriteArrayList remove() method in Java with Examples

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)

1. remove(int index)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

CopyOnWriteArrayList: [63, 54, 81, 96]
[63, 54, 96]

Program 2: This program involves CopyOnArrayList remove(int index) of String type

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

CopyOnWriteArrayList: [geeks, gfg, programming]
[gfg, programming]

2. remove(Object e)

The remove(Object o) method of CopyOnArrayList in Java is used to removes the first occurence 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

CopyOnWriteArrayList: [11, 22, 33, 22, 44]
[11, 33, 22, 44]

Program 2: This program involves CopyOnArrayList remove(Object o) of String type

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

CopyOnWriteArrayList: [geeks, gfg, programming]
[geeks, programming]


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.