Java集合——HashMap和Hashtable应用实例

本文探讨了Java中HashMap和Hashtable的应用,详细解释了当在HashMap的同一Key上添加不同Object时,如何发生覆盖现象。
摘要由CSDN通过智能技术生成
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值