Wednesday, July 3, 2024
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-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments