java map.entry 键值对_在 Java 中如何使用 entrySet 方法获取 HashMap 的所有键值对?...

Java HashMap 类的 entrySet 方法返回此 HashMap 对象中包含的键值对的集合。此方法返回的集合由原对象支持,因此,如果对集合进行任何更改,它将反映在 HashMap 中,反之亦然。

HashMap hmap = new HashMap();

hmap.put(1, "One");

hmap.put(2, "Two");

hmap.put(3, "Three");

System.out.println("HashMap contains: " + hmap);

/*

* To get all the mappings or entries contained in this hashmap, use

* the entrySet method.

*/

Set> entries = hmap.entrySet();

System.out.println("HashMap entries: " + hmap);

/*

* Changes you make to entry set will be reflected in the

* map, and vice versa.

*/

//remove a mapping from map

hmap.remove(2);

System.out.println("HashMap entries: " + hmap);

输出:

HashMap contains: {1=One, 2=Two, 3=Three}

HashMap entries: {1=One, 2=Two, 3=Three}

HashMap entries: {1=One, 3=Three}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值