Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

CopyOnWriteArrayList remove() method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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




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

Output:

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

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

Output:

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

Output:

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

My Personal Notes arrow_drop_up
Last Updated : 06 Jan, 2020
Like Article
Save Article
Similar Reads
Related Tutorials