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

}

 

 

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM