HashMap中使用entrySet()方法,底层抛析

博客详细分析了HashMap中使用entrySet()方法的条件和底层实现。通过创建Map存储Student对象及其对应的字符串,然后调用entrySet()方法获取Entry集合,并使用for-each循环遍历。entrySet()方法返回的是一个EntrySet对象,内部通过Iterator进行遍历,迭代过程中返回Node对象,由于Node实现了Map.Entry接口,因此可以以Map.Entry类型操作。
摘要由CSDN通过智能技术生成

HashMap中使用entrySet()方法,底层分析

条件:

Map<Student,String> hm=new HashMap<>();
Student s1=new Student(“xiaohei”,23);
Student s2=new Student(“dapang”,22);
hm.put(s1,“江苏”);
hm.put(s2,“北京”);
Set<Map.Entry<Student, String>> entries = hm.entrySet();
for (Map.Entry<Student, String> entry : entries) {
String value = entry.getValue();
Student key = entry.getKey();
System.out.println(key+"-------------"+value);
}

entrySet()方法分析:

1 **Set<Map.Entry<Student, String>> entries = hm.entrySet();
//方法源码如下:
public Set<Map.Entry<K, V>> entrySet(){
Set<Map.Entry<K, V>> es;
return (es=entrySet)==null?(es=new EntrySet()):es;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值