Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

CopyOnWriteArraySet remove() method in Java

  • Last Updated : 26 Nov, 2018

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

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:




// 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");
    }
}
Output:
CopyOnWriteArraySet: [32, 67, 98, 100]
Set after removal of 100 is: [32, 67, 98]

Program 2:




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




My Personal Notes arrow_drop_up
Recommended Articles
Page :