import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Student2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Student> m = new HashMap<String, Student>();
Student s1 = (new Student(18,"Lihua", "1"));
Student s2 = (new Student(20, "xiaohong", "2"));
Student s3 = (new Student(65, "xiaoMing", "3"));
m.put("111", s1);
m.put("222", s2);
m.put("333", s3);
//1
Set<String> kk = m.keySet();
Iterator<String> it = kk.iterator();
while(it.hasNext())
{
String k1 = it.next();
Student st = m.get(k1);
System.out.println(st);
}
//2
Set<Entry<String, Student>> en = m.entrySet();
Iterator<Entry<String, Student>> it2 = en.iterator();
while(it2.hasNext())
{
Entry<String, Student> ne = it2.next();
Student value = ne.getValue();
String key = ne.getKey();
System.out.println(key);
System.out.println(value);
}
//3
Collection<Student> c = m.values();
Iterator<Student> it3 = c.iterator();
while(it3.hasNext())
{
Student next = it3.next();
System.out.println(next);
}
}
}
java map展开
最新推荐文章于 2024-02-01 22:49:14 发布