CopyOnWriteArraySet remove() method in Java

The remove() method of CopyOnWriteArraySet removes the specified element if it is present in the set.

Syntax:

public boolean remove(Object o)

Parameters: The function accepts a mandatory parameter o which specifies the element to be removed from the set if present.

Return Value: The function returns true if set contains the specified element.

Below programs illustrate the above function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the
// remove() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<Integer> ArrSet
            = new CopyOnWriteArraySet<Integer>();
  
        // Add elements
        ArrSet.add(32);
        ArrSet.add(67);
        ArrSet.add(98);
        ArrSet.add(100);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // Remove using remove() function
        if (ArrSet.remove(100))
            System.out.println("Set after removal"
                               + " of 100 is: "
                               + ArrSet);
        else
            System.out.println("100 is not present");
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [32, 67, 98, 100]
Set after removal of 100 is: [32, 67, 98]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the
// isEmpty() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<String> ArrSet
            = new CopyOnWriteArraySet<String>();
  
        // Add elements
        ArrSet.add("gopal");
        ArrSet.add("geeks");
        ArrSet.add("geeks");
        ArrSet.add("technical");
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // Remove using remove() function
        if (ArrSet.remove("scripter"))
            System.out.println("Set after removal"
                               + " of 'scripter' is: "
                               + ArrSet);
        else
            System.out.println("'scripter' is not present");
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [gopal, geeks, technical]
'scripter' is not present

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#remove-java.lang.Object-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.