Map的输出依赖set(Map底层是Set)
HashMap:
模式3种
- entrySet();//把映射关系转化为set集合;
getValue() 返回与此项对应的值 getKey()返回与此项对应的键。
2.keySet();//把map中所有的Key放入Set集合
get() put();
3. values();//返回一个集合 此集合装map中的value
循环模式
- foreach
Set<Entry<String,String>> e= m.entrySet();
for (Entry<String, String> en : m.entrySet()) {
System.out.println(en.getKey()+"++++"+en.getValue());
}
=======================================================
for (String s : m.keySet()) {
System.out.println(s+"\t"+m.get(s));
}
=========================================================
for (Student s4 : m.values()) {
System.out.println(s4.name);
}
2.迭代器
Iterator<Entry<String,Student>> it=s.iterator();
while(it.hasNext()){
Entry<String,Student> s2=it.next();
System.out.println(s2.getKey()+"===="+s2.getValue().name);
}
================================================================
Iterator<String> it1=s3.iterator();
while(it1.hasNext()){
String s=it1.next();
System.out.println(m.get(s).name);
}
=============================================================
Collection<Student> cc= m.values();
Iterator<Student> it1 =cc.iterator();
while(it1.hasNext()){
Student ss= it1.next();

本文详细介绍了Java中Map接口的不同使用方式及其内部实现原理。重点讲解了HashMap的三种遍历模式:通过entrySet进行遍历、通过keySet获取所有键再获取对应值、直接遍历所有的值。每种模式都提供了具体的代码示例。
1150

被折叠的 条评论
为什么被折叠?



