HashMap用法 示例
2006-06-14 08:48
459 查看
/*
程序开始创建了一个散列映射,然后将名字的映射增加到平衡表。接下来,映射的内容通过使用由调用函数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"));
}
}
程序开始创建了一个散列映射,然后将名字的映射增加到平衡表。接下来,映射的内容通过使用由调用函数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"));
}
}
相关文章推荐
- HashMap用法 示例
- HashMap用法 示例
- OGNL, JSTL, STRUTS2标签用法示例 总结
- Linux中find常见用法示例
- cocos2d-js cc.DrawNode用法示例
- ftp命令常用用法示例
- 图像膨胀dilate函数用法示例
- HashMap,HashTable,TreeMap区别和用法
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- C# List 用法与示例
- hashmap的用法
- underscore用法,完整示例,喜欢就点赞加关注,以后会有更多惊喜
- Crosswalk基本用法示例(Demo)
- Linux中find常见用法示例
- Linux find 用法示例
- Linux中find常见用法示例
- 主干(trunk)、分支(branch )、标记(tag) 用法示例 + 图解
- Linux df 命令用法示例
- AWK用法示例 awk命令
- MySQL函数大全 及用法示例