当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 HashSet isEmpty() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。