Map的几种取值方法

public static void main(String[] args) throws IOException,ParseException {

Map<String,String> m = new HashMap<String,String>();

m.put("1", "haha");

m.put("2", "hehe");

m.put("3", "heihei");

 //第一种

for(String k : m.keySet())

{

System.out.println(k+m.get(k));

}

 //第二种

for(Map.Entry<String, String> d:m.entrySet())

{

System.out.println(d.getValue()+d.getKey());

}

 //第三种

Set x = m.keySet();

Iterator it = x.iterator();

while(it.hasNext())

{

String u = (String) it.next();

System.out.println(u+m.get(u));

}

 //第四种

Set<Map.Entry<String, String>> y = m.entrySet();

Iterator<Map.Entry<String, String>> it1 = m.entrySet().iterator();

while(it1.hasNext())

{

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

System.out.println(en.getKey()+en.getValue());

}

}

评论

暂无评论