HashMap初始化以及遍历的三种方式

public static void main(String[] args){

Map<String, String> map = new HashMap<String, String>(){

{

put("zhang","xinxin");

put("wnag", "jinfeng");

put("li", "xuemei");

put("zhao", "xinyi");

}

};

System.out.println("NO1.===========================");

for(Entry<String, String> entry : map.entrySet())

{

System.out.println("key: " + entry.getKey() + "    value: " + entry.getValue() );

}

System.out.println("NO2.===========================");

for (String key : map.keySet()) 

{

System.out.println("key: " + key + "    value: " + map.get(key));

}

System.out.println("NO3.===========================");

Iterator<Entry<String, String>> it = map.entrySet().iterator();

while (it.hasNext()) {

Entry<String, String> en = it.next();

System.out.println("key: " + en.getKey() + "    value: " + en.getValue() );

}

}


你可能感兴趣的:(HashMap,遍历,初始)