HashMap用法 示例

/*

程序开始创建了一个散列映射,然后将名字的映射增加到平衡表。接下来,映射的内容通过使用由调用函数entrySet()而获得的集合“视图”而显示出来。关键字和值通过调用由Map.Entry定义的getKey()和getValue()方法而显示。注意存款是如何被制成Evan的账目的。put()方法自动用新值替换与指定关键字相关联的原先值。

*/

import java.util.*;
class HaspMapDemo{
 public static void main(String[] args)
 {
  //Create a hasp map
  HashMap   hm=new   HashMap();
  //Put elements to the map
  hm.put("Evan",new Double(12345.77));
  hm.put("Rose",new Double(78777));
  hm.put("Magic",new Double(-99.10));
  hm.put("Mike",new Double(100.00));
  hm.put("Sue",new Double(17.15));
  //Get a set of the entries
  Set set = hm.entrySet();
  //Get an iterator
  Iterator itr = set.iterator();
  //Display elements
  while (itr.hasNext()){
   Map.Entry me = (Map.Entry)itr.next();
   System.out.println(me.getKey() + ": ");
   System.out.println(me.getValue());
  }
  System.out.println();
  //Deposit 1000 into Evan's account
  double balance = ((Double)hm.get("Evan")).doubleValue();
  hm.put("Evan",new Double(balance + 1000));
  System.out.println("Evan's new balance : " + hm.get("Evan"));
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值