package HashMapTest;

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

public class HashMapTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HashMapTest test = new HashMapTest();
        test.input();
        test.get2();
    }
    
    private Map<Integer,String> map;
    private final String[] strArray = {"aaa","bbb","ccc","ddd","eee"};
    
    private void input()
    {
        map = new HashMap<Integer,String>();
        
        for(int i=0;i<5;i++)
        {
            map.put(i, strArray[i]);
        }
    }
    
    //遍历方法一 hashmap entrySet() 遍历
    private void get()
    {
        Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext())
        {
            Map.Entry<Integer,String> entry = (Map.Entry<Integer,String>)iterator.next();
            int key = entry.getKey();
            String value = entry.getValue();
            
            System.out.println("key="+key+",value="+value);
        }
    }
    
    //遍历方法二 JDK1.5中,应用新特性For-Each循环
    private void get2()
    {
        for(Map.Entry<Integer, String> entry:map.entrySet())
        {
            int key = entry.getKey();
            String value = entry.getValue();
            
            System.out.println("key2="+key+",value2="+value);
        }
    }
}

 

posted on 2016-04-15 11:35  Android9527  阅读(369)  评论(0编辑  收藏  举报