java运行问题3

来源:百度知道 编辑:UC知道 时间:2024/05/24 13:38:21
import java.util.*;
class HashMapDemo
{
public static void main(String[] args)
{
HashMap hm=new HashMap();
hm.put("John Doe",new Double(3434.34));
hm.put("Tom Smith",new Double(123.22));
hm.put("Jane Baker",new Double(1378.00));
hm.put("Todd Hall",new Double(99.22));
hm.put("Ralph Smith",new Double(-19.08));
Set set=hm.entrySet();//显示内容
Iterator i=set.iterator();//返回调用集合的迭代程序
while(i.hasNext())
{ Map.Entry me=(Map.Entry)i.next();
System.out.println(me.getKey()+": ");
System.out.println(me.getValue());}
System.out.println();
double balance=((Double)hm.get("John Doe")).doubleValue();//调用John Doe的关键字和值
hm.put("John Doe",new Double(balance+1000));原值加1000
System.out.println("John Doe's new balance: "+hm.get("John Doe"));

Set和Map都是Java里很基本,很常用的数据结构类型。此外还有List等等。
顾名思义,Set就是集合,Map就是图(映射)。

Set set = hm.entrySet(); 是将Map结构转化为Set结构。

在Set(集合)里,想要遍历此集合的话,就要有一个Iterator,
i.hasNext();就是判断看此集合中是否还有未遍历的元素。有的话,返回true,否则返回false;

Map.Entry是一个接口,类似于映射项(键-值对)。
Map.Entry me=(Map.Entry)i.next(); 这句话就是将,Set中的下一项拿出来(i.next()),并把它转化成为一个Map.Entry对象,而后负给me对象。

兄弟,这都是一些基础的东东。读一读基本教程吧,会对你大有帮助的。