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");
方法一:
while(keys.hasnext()) { } 方法二: while(keys.hasnext()) { }
iterator
string key = (string) keys.next();
string value=map.get(key);
system.out.println("键"+key+"="+"值"+value);
iterator keys = map.entryset().iterator();
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));
上一篇: 红干椒你爱吃吗,看看都有哪些类型