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

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

Java中的 ConcurrentSkipListSet size() 方法

Java .util.concurrent.ConcurrentSkipListSet.size()方法是Java中的一个内置函数,它给出集合中存在的元素的总数。

句法:

ConcurrentSkipListSet.size()

参数:该函数不接受任何参数。

返回值:函数返回队列中元素的数量。

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

方案一:

// Java Program Demonstrate size()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetSizeExample1 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet set = 
                       new ConcurrentSkipListSet();
  
        // Adding elements to this set
        for (int i = 1; i <= 10; i++)
            set.add(i);
  
        // Printing the size of the set
        System.out.println("Number of elements in the set = "
                           + set.size());
        // Printing the elements
        System.out.println("set : " + set);
    }
}
输出:
Number of elements in the set = 10
set : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

方案二:

// Java Program Demonstrate size()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetSizeExample2 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet set = 
                         new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add("A");
        set.add("B");
        set.add("C");
        set.add("D");
        set.add("E");
  
        // Printing the size of the set
        System.out.println("Number of elements in the set = "
                           + set.size());
        // Printing the elements
        System.out.println("set : " + set);
    }
}
输出:
Number of elements in the set = 5
set : [A, B, C, D, E]

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#size–