Friday, December 27, 2024
Google search engine
HomeLanguagesJavaCopyOnWriteArraySet remove() method in Java

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:




// 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-

RELATED ARTICLES

Most Popular

Recent Comments