java关于hashmap的问题,望高手来解答

来源:百度知道 编辑:UC知道 时间:2024/06/27 07:38:39
我现在利用put方法给hashmap容器给了两组值,比如:
int i=1,j=2;
String st="bein";
map.put(i, st);
map.put(j, st);
然后我想利用迭代器(iterator)依次从hashmap容器的第一组值取出来访问,请问怎么实现?

Map <Integer,String> c = new HashMap <Integer,String>();
c.put(1, "HashMap-1");
c.put(2, "HashMap-2");
c.put(3, "HashMap-3");
c.put(4, "HashMap-4");
c.put(5, "HashMap-5");
c.put(6, "HashMap-6");

Iterator<Entry<Integer, String>> it = c.entrySet().iterator();
while(it.hasNext()){

Map.Entry<Integer, String> entry = (Entry<Integer, String>) it.next();

Object key = entry.getKey();
Object value = entry.getValue();

System.out.print(key +"->"+ value + " ");
}