java 遍歷map

package com.yu;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class EntrySetTest {
    public static void main(String[] args){
        Map<String,String> map = new HashMap<String,String>();
        map.put("1","劍聖");
        map.put("2","卡爾");
        map.put("3","影魔");
        map.put("4","屠夫");
        Iterator i = map.entrySet().iterator();
        while(i.hasNext()){
            Map.Entry<String,String> entry = (Entry<String,String>)i.next();
            System.out.println("key: " + entry.getKey() + "--value: " + entry.getValue());
        }
    }
}
/*output:
key: 3--value: 影魔
key: 2--value: 卡爾
key: 1--value: 劍聖
key: 4--value: 屠夫*/
相關文章
相關標籤/搜索