代码改变世界

JAVA基础篇—HashMap

2017-08-19 20:24  lc_java  阅读(200)  评论(0编辑  收藏  举报

/class Depositor

package 银行储户;

public class Depositor {
	private String id;
	private String name;
	private double balance;

	public Depositor(String id,String name,double bal) {
		// TODO Auto-generated constructor stub
		super();
		this.id=id;
		this.name=name;
		this.balance = bal;
	}
	public double getBalance() {
		return balance;
		}
		public void setBalance(double balance) {
		this.balance = balance;
		}
		public String getId() {
		return id;

		}
		public void setId(String id) {
		this.id = id;
		}
		public String getName() {
		return name;
		}
		public void setName(String name) {
			this.name=name;
		}
    
}

  /class TestMap

package 银行储户;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;



public class TestMap {
	public double getBalance(Map depositor,String id){ 
		System.out.print("id:"+id);
	return depositor.get(id).hashCode();

	}
	public static void main(String args[]) {
		Map h = new HashMap(); Depositor a1 = new Depositor("1001","zhangsan",12345); Depositor a2 = new Depositor("1002","tom",8888.50);
		Depositor a3 = new Depositor("1003","mary",6666);
		h.put("1001", a1);
		h.put("1002", a2);
		h.put("1003", a3);

		TestMap hme = new TestMap();

		
		//map
		Set keySet= h.keySet();
		Iterator it = keySet.iterator();
		double sum = 0;
		while(it.hasNext()){
		Object key = it.next();
		Depositor a = (Depositor) h.get(key);
		System.out.println("id:"+a.getId()+"Balance"+a.getBalance()); //
		sum += a.getBalance();
		}
		System.out.println("sum:"+sum);

		}
}