Map.entry 和Map.entrySet

public class MapTest {
HashMap map = new HashMap();

public MapTest() {// 为简单起见,手动生成几个,如果采用生成器,更方便
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
}

public static void main(String[] args) {
MapTest mt = new MapTest();
for (Map.Entry entry : mt.map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}

Iterator it = mt.map.entrySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

像上面的程序显示所示,Map.entry可以看作是Map.entrySet的一个对象,可以通过for循环取,也可以用迭代器取其中的元素。
Map.entrySet集合中就是map所存内容,每个Map.entryu对象都只是存储了他的索引,而不是实际的键和值。
在迭代中entry对象被用作数据的视窗,它只包含在静态字符串数组中的索引。在每次调用next方法时,index会递增指向下一个元素。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值