欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java迭代Map方法

程序员文章站 2023-04-04 13:32:11
Map map=new HashMap(); map.put("1", "one"); map.put("2","two"); map.put("3", "three"); map.put("4", "four"); map.put("5", "five"); map.put("6", "six") ......

map map=new hashmap();
map.put("1", "one");
map.put("2","two");
map.put("3", "three");
map.put("4", "four");
map.put("5", "five");
map.put("6", "six");

方法一:
iterator keys = map.keyset().iterator();

while(keys.hasnext()) {
string key = (string) keys.next();
string value=map.get(key);
system.out.println("键"+key+"="+"值"+value);

}

方法二:
iterator keys = map.entryset().iterator();

while(keys.hasnext()) {
map entry =(map)keys.next();
string key=(string)entry.getkey();
string value=(string)entry.getvalue();
system.out.println("键"+key+"="+"值"+value);

}
方法三:
map.foreach((key, value) -> system.out.println(key + ":" + value));