【Java学习笔记】Map借口的子接口----HashMap

存储在HashMap集合中的元素,必须覆盖hashCode和equals方法(与HashSet类似)

 

 import java.util.HashMap;
import java.util.Iterator; import cn.itcast.p2.bean.Student; public class HashMapDemo { public static void main(String[] args) {
/*
* 将学生对象和学生的归属地通过键与值存储到map集合中
*/
HashMap<Student,String> hm = new HashMap<Student,String>(); hm.put(new Student("lisi",38), "北京");
hm.put(new Student("zhaoliu",24), "上海");
hm.put(new Student("xiaoqiang",31), "沈阳");
hm.put(new Student("wangcai",38), "大连");
hm.put(new Student("zhaoliu",24), "铁岭"); Iterator<Student> it = hm.keySet().iterator();
while (it.hasNext())
{
Student key = it.next();
String value = hm.get(key);
System.out.println(key.getName()+":"+key.getAge()+"--"+value);
} } }

【Java学习笔记】Map借口的子接口----HashMap

上一篇:Java集合类学习笔记(Map集合)


下一篇:Java 学习笔记 两大集合框架Map和Collection