package com.shuzu;
import java.util.HashMap;
import java.util.Iterator;
public class HashMapTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 = new Student("小明",25);
Student s2 = new Student("小刚",24);
Student s3 = new Student("小胖",23);
HashMap hm = new HashMap();
hm.put("小明", s1);
hm.put("小刚", s2);
hm.put("小强", s3);
//1.取出信息
if(hm.containsKey("小明")){
System.out.println("有该学生!");
Student st = (Student) hm.get("小明");
System.out.println("姓名:"+st.getName()+" 年龄:"+st.getAge());
}else {
System.out.println("没有该学生!");
}
//2.利用 Iterator ,遍历 HashMap
Iterator it = hm.keySet().iterator();
while(it.hasNext()){
String key = it.next().toString();
//上一步得到 “Key”,现在取出对象
Student st = (Student) hm.get
Java集合——HashMap和Hashtable应用实例
最新推荐文章于 2024-07-04 10:00:00 发布
本文探讨了Java中HashMap和Hashtable的应用,详细解释了当在HashMap的同一Key上添加不同Object时,如何发生覆盖现象。
摘要由CSDN通过智能技术生成