Java中的HashSet isEmpty()函数
Java.util.HashSet.isEmpty()函数用于检查一个HashSet是否为空。如果HashSet为空则返回True,否则返回False。
语法:
Hash_Set.isEmpty()
参数: 此方法不接受任何参数。
返回值: 如果集合为空,则返回True,否则返回False。
下面的程序说明了Java.util.HashSet.isEmpty()函数:
// Java code to illustrate contains()
import java.io.*;
import java.util.HashSet;
public class HashSetDemo {
public static void main(String args[])
{
// Creating an empty HashSet
HashSet<String> set = new HashSet<String>();
// Use add() method to add elements into the Set
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("4");
set.add("Geeks");
// Displaying the HashSet
System.out.println("HashSet: " + set);
// Check for the empty set
System.out.println("Is the set empty: " + set.isEmpty());
// Clearing the set using clear() method
set.clear();
// Again Checking for the empty set
System.out.println("Is the set empty: " + set.isEmpty());
}
}
HashSet: [4, Geeks, Welcome, To]
Is the set empty: false
Is the set empty: true