Map的幾種取值方法

 

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

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

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

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

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

 

 

 //第一種next

for(String k : m.keySet())margin

{static

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

}while

 

 //第二種

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

{

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

}

 

 //第三種

Set<String> 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());

}

 

 

}

相關文章
相關標籤/搜索