java isempty用法_Java Dictionary isEmpty()用法及代码示例

字典类的isEmpty()方法检查此字典是否具有任何键-值映射。仅当此词典中没有条目时,该函数才返回TRUE。

用法:

public abstract boolean isEmpty()

返回值:如果字典为空,则该函数返回TRUE,否则返回FALSE。

异常:该函数不会引发异常。

以下示例程序旨在说明Dictionary.isEmpty()方法的使用:

示例1:

// Java Program to illustrate

// Dictionary.isEmpty() method

import java.util.*;

class GFG {

public static void main(String[] args)

{

// Create a new hashtable

Dictionary

d = new Hashtable();

// Insert elements in the hashtable

d.put(1, "Geeks");

d.put(2, "for");

d.put(3, "Geeks");

// Print the Dictionary

System.out.println("\nDictionary: " + d);

// check if this dictionary is empty

// using isEmpty() method

if (d.isEmpty()) {

System.out.println("Dictionary "

+ "is empty");

}

else

System.out.println("Dictionary "

+ "is not empty");

}

}

输出:

Dictionary: {3=Geeks, 2=for, 1=Geeks}

Dictionary is not empty

示例2:

// Java Program to illustrate

// Dictionary.isEmpty() method

import java.util.*;

class GFG {

public static void main(String[] args)

{

// Create a new hashtable

Dictionary

d = new Hashtable();

// Print the Dictionary

System.out.println("\nDictionary: " + d);

// check if this dictionary is empty

// using isEmpty() method

if (d.isEmpty()) {

System.out.println("Dictionary "

+ "is empty");

}

else

System.out.println("Dictionary "

+ "is not empty");

// Insert elements in the hashtable

d.put("a", "GFG");

d.put("b", "gfg");

// Print the Dictionary

System.out.println("\nDictionary: " + d);

// check if this dictionary is empty

// using isEmpty() method

if (d.isEmpty()) {

System.out.println("Dictionary "

+ "is empty");

}

else

System.out.println("Dictionary "

+ "is not empty");

// Remove elements in the hashtable

d.remove("a");

d.remove("b");

// Print the Dictionary

System.out.println("\nDictionary: " + d);

// check if this dictionary is empty

// using isEmpty() method

if (d.isEmpty()) {

System.out.println("Dictionary "

+ "is empty");

}

else

System.out.println("Dictionary "

+ "is not empty");

}

}

输出:

Dictionary: {}

Dictionary is empty

Dictionary: {b=gfg, a=GFG}

Dictionary is not empty

Dictionary: {}

Dictionary is empty

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值