• JAVA基础篇—HashMap


    /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);
    
    		}
    }
    

      

  • 相关阅读:
    OC-字典
    作业
    block语法排序 遍历
    oc-NSArray
    oc之获取系统当前时间的方法
    修改mysql的默认字符集
    mysql查询结果添加序列号
    PHP Socket 编程过程详解
    一篇详细的 Mysql Explain 详解
    阿里云云主机挂载数据盘,格式化硬盘(新购云主机)(转)
  • 原文地址:https://www.cnblogs.com/lc-java/p/7397805.html
Copyright © 2020-2023  润新知