首页 > 代码库 > hashmap的遍历

hashmap的遍历

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的遍历