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

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

Java中的 ConcurrentSkipListSet add() 方法

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

句法:

ConcurrentSkipListSet.add(E e)

参数:该函数接受单个参数e ,即要插入的元素。

返回值:该函数返回一个 True 布尔值。

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

程序1:在集合中添加整数。

// Java program to demonstrate add()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetAddExample1 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet Lset = 
                  new ConcurrentSkipListSet();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            Lset.add(i);
  
        // Printing elements of the set
        System.out.println("The set contains: ");
        for (Integer i : Lset)
            System.out.print(i + " ");
    }
}
输出:
The set contains: 
10 20 30 40 50

程序 2:在集合中添加字符串。

// Java program to demonstrate add()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetAddExample2 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet Lset = 
                     new ConcurrentSkipListSet();
  
        // Adding elements to this set
        Lset.add("alex");
        Lset.add("bob");
        Lset.add("chuck");
        Lset.add("drake");
        Lset.add("eric");
  
        // Printing elements of the set
        System.out.println("The set contains: ");
        for (String i : Lset)
            System.out.print(i + " ");
    }
}
输出:
The set contains: 
alex bob chuck drake eric

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#add(E)