map 的初始化和遍历

时间:2023-01-30 19:47:30

标记一下:

初始化map

Map<String,String> prod = new HashMap<String,String>(){//构造方法

{//构造代码块儿

put("420001056337","X00241003");

put("420001056321","X00241004");

put("420001056338","X00241005");

put("420001056322","X00241006");

put("420001056341","X00241009");

}

};

遍历map

Iterator it = prod.entrySet().iterator();

 

if(it.hasNext()){

       Map.Entry entry = (Entry) it.next();

String value = (String) entry.getValue();//得到value值String key= (String) entry.getKey();//得到key

}

 

 

可以用Set<String> key = prod.keySet();

通过遍历set:

 for(Iterator it = key.iterator();it.hasNext();){

System.out.println(it.next());

}

这样也可以遍历map