Java的Map定义

package com.java.map.test;  
  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  
  
public class MapTest {  
    public static Map<String,String> addMap(){  
        Map<String,String> map=new HashMap<String,String>();  
        for(int i=0;i<10;i++){  
            String name="name"+i;  
            if(!map.containsKey(name)){  
                String value="john"+i;  
                map.put(name, value);  
            }  
            String sex="sex"+i;  
            if(!map.containsKey(sex)){  
                String value="female"+i;  
                map.put(sex, value);  
            }  
        }  
        return map;  
    }  
    public static void main(String[] args) {  
        Map<String,String> map=new HashMap<String,String>();  
        new MapTest();
        map=MapTest.addMap();  
        Iterator<String> it=map.keySet().iterator();  
        Object key=null;  
        Object value=null;  
        while(it.hasNext()){  
            key=it.next();  
            value=map.get(key);  
            System.out.println(key+":"+value);  
        }  
    }  
  
}  

 

posted @ 2015-04-23 14:56  KOYOU  阅读(6465)  评论(0编辑  收藏  举报