📌  相关文章
📜  Java中的 ConcurrentSkipListSet remove() 方法

📅  最后修改于: 2022-05-13 01:54:47.999000             🧑  作者: Mango

Java中的 ConcurrentSkipListSet remove() 方法

Java .util.concurrent.ConcurrentSkipListSet.remove() 方法是Java中的一个内置函数,用于删除该集合中存在的元素。

句法:

ConcurrentSkipListSet.remove(Object o)

参数:该函数接受单个参数即要删除的对象。

返回值:该函数在成功删除对象时返回一个 true 布尔值,否则返回 false。

下面的程序说明了 ConcurrentSkipListSet.remove() 方法:

程序 1:要删除的元素存在于集合中。

// Java Program Demonstrate remove()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetRemoveExample1 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet set = 
                         new ConcurrentSkipListSet();
  
        // 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 + " ");
    }
}
输出:
The elements in the set are:
1 2 3 4 5 
Remaining elements in set : 
2 3 4

程序 2:要删除的元素在集合中不存在。

// Java Program Demonstrate remove()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetRemoveExample2 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet set =
                       new ConcurrentSkipListSet();
  
        // 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 + " ");
    }
}
输出:
The elements in the set are:
10 11 12 13 14 15 
Remaining elements in set : 
10 11 12 13 14 15

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#remove-java.lang.Object-