首页 > 编程知识 正文

判断arraylist是否为空,java如何判断map集合为空

时间:2023-05-05 12:45:18 阅读:160049 作者:2254

使用此isEmpty ()方法检查HashMap是否为空。 首先,让我们创建HashMap-HashMaphm=newHashMap ();

现在,添加一些元素-hm.put('bag ',newinteger ) (1100 ) )。

hm.put('Wallet ',newinteger ) ) 700 );

hm.put('Belt ',newinteger ) ) 600 );

因为我们在上面添加了元素,所以HashMap不是空的。 让我检查一下-set.isEmpty (

以下是检查HashMap是否为空的示例-

示例importjava.util.*;

公共类demo {

publicstaticvoidmain (string args [ ] ) {

//创建哈希地图

HashMaphm=newHashMap (;

//将元素放入映射中

hm.put('bag ',newinteger ) ) 1100 );

hm.put('Wallet ',newinteger ) ) 700 );

hm.put('Belt ',newinteger ) ) 600 );

//获取一组条目

Setset=hm.entrySet (;

//获取迭代器

Iteratori=set.iterator (;

//显示元素

wile(I.Hasnext ) ) ) )。

map.entryme=(map.entry ) i.next );

system.out.print(me.getkey () ' : );

system.out.println(me.getvalue () );

}

System.out.println (;

system.out.println (isthehashmapwithemptyelements? ' set.isEmpty ();

}

}

输出结果Belt:600

Wallet:700

Bag:1100

IstheHashMapwithemptyelements? 假

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。