Java中的 CopyOnWriteArraySet size() 方法
CopyOnWriteArraySet的size()方法返回 Set 的大小。它返回当前 Set 中的元素数。
句法:
public int size()
返回值:函数返回当前Set中的元素个数。
下面的程序说明了上述函数:
方案一:
// Java Program to illustrate the CopyOnWriteArraySet
// size() 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 ArrSet
= new CopyOnWriteArraySet();
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet: "
+ ArrSet);
// size value print
System.out.println("size of ArrSet: "
+ ArrSet.size());
// Add elements
ArrSet.add(32);
ArrSet.add(67);
ArrSet.add(98);
ArrSet.add(100);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet: "
+ ArrSet);
// size value print
System.out.println("size of ArrSet: "
+ ArrSet.size());
}
}
输出:
CopyOnWriteArraySet: []
size of ArrSet: 0
CopyOnWriteArraySet: [32, 67, 98, 100]
size of ArrSet: 4
方案二:
// Java Program to illustrate the CopyOnWriteArraySet
// size() 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 ArrSet
= new CopyOnWriteArraySet();
// Add elements
ArrSet.add("gopal");
ArrSet.add("gfg");
ArrSet.add("jgec");
ArrSet.add("sudo");
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet: "
+ ArrSet);
// size value print
System.out.println("size of ArrSet: "
+ ArrSet.size());
}
}
输出:
CopyOnWriteArraySet: [gopal, gfg, jgec, sudo]
size of ArrSet: 4
参考: https: Java/util/concurrent/CopyOnWriteArraySet.html#size–