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;