zoukankan      html  css  js  c++  java
  • 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);
    
    		}
    }
    

      

  • 相关阅读:
    OAuth2.0的四种授权模式
    Jedis整合单机、Sentinel和Cluster模式
    Redis Cluste部署
    Web-动态页面
    Web开发-Servlet&HTTP&Request
    Ajax&Json
    Web开发之Tomcat&Servlet
    PagedListCore的使用
    自己写一个依赖注入容器Container
    在core2.0中实现按程序集注入依赖
  • 原文地址:https://www.cnblogs.com/lc-java/p/7397805.html
Copyright © 2011-2022 走看看