map遍历的方式有4种,分别是:1、使用for循环遍历map;2、使用迭代遍历map;3、使用keyset迭代遍历map;4、使用entryset遍历map。
遍历map的几种方式如下:
(学习视频分享:java教学视频)
java代码:
map<string,string> map=new hashmap<string,string>(); map.put("username", "qq"); map.put("password", "123"); map.put("userid", "1"); map.put("email", "qq@qq.com");
方法一、for循环
for(map.entry<string, string> entry:map.entryset()){ system.out.println(entry.getkey()+"--->"+entry.getvalue()); }
方法二、迭代
set set = map.entryset(); iterator i = set.iterator(); while(i.hasnext()){ map.entry<string, string> entry1=(map.entry<string, string>)i.next(); system.out.println(entry1.getkey()+"=="+entry1.getvalue()); }
方法三、keyset()迭代
iterator it=map.keyset().iterator(); while(it.hasnext()){ string key; string value; key=it.next().tostring(); value=map.get(key); system.out.println(key+"--"+value); }
方法四、entryset()迭代
iterator it=map.entryset().iterator(); system.out.println( map.entryset().size()); string key; string value; while(it.hasnext()){ map.entry entry = (map.entry)it.next(); key=entry.getkey().tostring(); value=entry.getvalue().tostring(); system.out.println(key+"===="+value); } for (map.entry<string, string> entry : map.entryset()) { system.out.println("key= " + entry.getkey() + " and value= " + entry.getvalue()); }
相关推荐:java入门教程
以上就是map遍历的几种方式分别是什么的详细内容。