码迷,mamicode.com
首页 > 其他好文 > 详细

hashmap的遍历

时间:2014-10-16 16:40:02      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:ar   java   on   line   new   size   as   text   class   

import java.util.HashMap;
import java.util.Iterator;

public class hash {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, String>hm=new HashMap<String, String>();
hm.put("100","001");
hm.put("200","002");
hm.put("300","003");
hm.put("400","004");
hm.put("500","005");
hm.put("600","006");
hm.put("700","007");
hm.put("800","008");
hm.put("900","009");
System.out.println(hm.get("100"));
System.out.println(hm.size());
}
}
方式1
Iterator iterator = hm.keySet().iterator();
while(iterator.hasNext()) {
System.out.println(hm.get(iterator.next()));
}
方式2
Set set = hm.entrySet() ;
java.util.Iterator it = hm.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.println(entry.getValue());
}

hashmap的遍历

标签:ar   java   on   line   new   size   as   text   class   

原文地址:http://www.cnblogs.com/xgjblog/p/4028859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!