📜  Java中的 CopyOnWriteArraySet isEmpty() 方法

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

Java中的 CopyOnWriteArraySet isEmpty() 方法

CopyOnWriteArraySetisEmpty(E e)方法检查 Set 是否为空。如果 Set 为空,则返回 true,否则返回 false。

句法:

public boolean isEmpty()

返回值:如果Set为空,该函数返回true ,否则返回false。

下面的程序说明了上述函数:

方案一:

Java
// Java Program to illustrate the
// isEmpty() method in CopyOnWriteArraySet
 
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(32);
        ArrSet.add(67);
        ArrSet.add(98);
        ArrSet.add(100);
 
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: " + ArrSet);
 
        // check using function
        if (ArrSet.isEmpty())
            System.out.println("\nSet is empty");
        else
            System.out.println("\nSet is not empty");
    }
}


Java
// Java Program to illustrate the
// isEmpty() method in CopyOnWriteArraySet
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);
 
        // check using function
        if (ArrSet.isEmpty())
            System.out.println("\nSet is empty");
        else
            System.out.println("\nSet is not empty");
    }
}


输出:
CopyOnWriteArraySet: [32, 67, 98, 100]

Set is not empty

方案二:

Java

// Java Program to illustrate the
// isEmpty() method in CopyOnWriteArraySet
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);
 
        // check using function
        if (ArrSet.isEmpty())
            System.out.println("\nSet is empty");
        else
            System.out.println("\nSet is not empty");
    }
}
输出:
CopyOnWriteArraySet: []

Set is empty

参考: https: Java/util/concurrent/CopyOnWriteArraySet.html#isEmpty–