Wednesday, July 3, 2024
HomeLanguagesJavaConcurrentSkipListSet remove() method in Java

ConcurrentSkipListSet remove() method in Java

The java.util.concurrent.ConcurrentSkipListSet.remove() method is an in-built function in Java which is used to remove an element if it is present in this set.

Syntax:

ConcurrentSkipListSet.remove(Object o)

Parameters: The function accepts a single parameter o i.e. the object to be removed.

Return Value: The function returns a true boolean value on successful removal of the object, otherwise returns false.

Below programs illustrate ConcurrentSkipListSet.remove() method:

Program 1: The element to be removed is present in the set.




// Java Program Demonstrate remove()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetRemoveExample1 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet<Integer> set = 
                         new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 1; i <= 5; i++)
            set.add(i);
  
        // Printing the elements of the set
        System.out.println("The elements in the set are:");
        for (Integer i : set)
            System.out.print(i + " ");
  
        // remove() method will remove the specified
        // element from the set
        set.remove(1);
        set.remove(5);
  
        // Printing the elements of the set
        System.out.println("\nRemaining elements in set : ");
        for (Integer i : set)
            System.out.print(i + " ");
    }
}


Output:

The elements in the set are:
1 2 3 4 5 
Remaining elements in set : 
2 3 4

Program 2: The element to be removed is not present in the set.




// Java Program Demonstrate remove()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetRemoveExample2 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet<Integer> set =
                       new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 15; i++)
            set.add(i);
  
        // Printing the elements of the set
        System.out.println("The elements in the set are:");
        for (Integer i : set)
            System.out.print(i + " ");
  
        // remove() method will remove the specified
        // element from the set
        set.remove(1);
        set.remove(5);
  
        // Printing the elements of the set
        System.out.println("\nRemaining elements in set : ");
        for (Integer i : set)
            System.out.print(i + " ");
    }
}


Output:

The elements in the set are:
10 11 12 13 14 15 
Remaining elements in set : 
10 11 12 13 14 15

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.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