如何遍历map集合
发布于2018-11-26 20:49:59
Map集合是基于java核心类——java.util中的;
Map集合用于储存元素对,Map储存的是一对键值(key和value),是通过key映射到它的value
values() : 是获取集合中的所有的值----没有键,没有对应关系。
KeySet() : 将Map中所有的键存入到set集合中。因为set具备迭代器。所有可以迭代方式取出所有的键,再根据get方法。获取每一个键对应的值。 迭代后只能通过get()取key 。
entrySet():是返回此映射中包含的映射关系的 Set 视图。 Map.Entry表示映射关系,迭代后可以e.getKey(),e.getValue()取key和value。返回的是Entry接口 。
for each遍历
-
import java.util.Iterator;
-
import java.util.Map;
-
-
/**
-
* @Title: mapTest
-
* @Description:
-
* @author 阿伟
-
* @createTime 2018年11月26日下午7:31:03
-
*/
-
public
class mapTest {
-
public
static void main(
String[] args) {
-
Map<
String,
String>
map = new
HashMap<
String,
String>();
-
map.put(
"student1",
"阿伟");
-
map.put(
"student2",
"小李");
-
map.put(
"student3",
"小张");
-
map.put(
"student4",
"小王");
-
//
-
// //1.使用entrySet()遍历
-
System.out.
println(
"使用entrySet()遍历");
-
Iterator it =
map.entrySet().iterator();
-
while (it.hasNext()) {
-
Map.
Entry entry =(
Map.
Entry) it.next();
-
Object key = entry.getKey();
-
Object value = entry.getValue();
-
System.out.
println(
"key="+key+
" value"+value);
-
-
}
-
//2.通过Map.Keyset遍历key和value,普遍使用,二次取值
-
System.out.
println(
"通过Map.Keyset遍历key和value,普遍使用,二次取值");
-
for(
String key:
map.keySet()){
-
System.out.
println(
"Key="+key+
"\tvalue="+
map.
get(key));
-
}
-
//3通过map.values()遍历所有的value,但不能遍历key
-
System.out.
println(
"通过map.values()遍历所有的value,但不能遍历key");
-
for(
String v:
map.values()){
-
System.out.
println(
"value="+v);
-
}
-
//4通过map.entrySet遍历key和value(推荐使用,特别是容量大时)
-
System.out.
println(
"通过map.entrySet遍历key和value(推荐使用,特别是容量大时)");
-
for(
Map.
Entry<
String,
String> entry:
map.entrySet()){
-
System.out.
println(
"key="+entry.getKey()+
"\tvalue="+entry.getValue());
-
}
-
-
}
-
-
}
--------------------测试结果-----------------------------------
有一个疑问,为什么遍历顺序为2143 ?请大神回答
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">2</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/Ren_gw"> <img src="https://profile.csdnimg.cn/9/2/C/3_ren_gw" class="avatar_pic" username="Ren_gw"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/Ren_gw" data-report-click="{"mod":"popu_379"}" target="_blank">Ren_gw</a></span> </div> <div class="text"><span>发布了33 篇原创文章</span> · <span>获赞 10</span> · <span>访问量 1万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=Ren_gw" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>