如何遍历map集合

如何遍历map集合

 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遍历


 
 
  1. import java.util.Iterator;
  2. import java.util.Map;
  3. /**
  4. * @Title: mapTest
  5. * @Description:
  6. * @author 阿伟
  7. * @createTime 2018年11月26日下午7:31:03
  8. */
  9. public class mapTest {
  10. public static void main( String[] args) {
  11. Map< String, String> map = new HashMap< String, String>();
  12. map.put( "student1", "阿伟");
  13. map.put( "student2", "小李");
  14. map.put( "student3", "小张");
  15. map.put( "student4", "小王");
  16. //
  17. // //1.使用entrySet()遍历
  18. System.out. println( "使用entrySet()遍历");
  19. Iterator it = map.entrySet().iterator();
  20. while (it.hasNext()) {
  21. Map. Entry entry =( Map. Entry) it.next();
  22. Object key = entry.getKey();
  23. Object value = entry.getValue();
  24. System.out. println( "key="+key+ " value"+value);
  25. }
  26. //2.通过Map.Keyset遍历key和value,普遍使用,二次取值
  27. System.out. println( "通过Map.Keyset遍历key和value,普遍使用,二次取值");
  28. for( String key: map.keySet()){
  29. System.out. println( "Key="+key+ "\tvalue="+ map. get(key));
  30. }
  31. //3通过map.values()遍历所有的value,但不能遍历key
  32. System.out. println( "通过map.values()遍历所有的value,但不能遍历key");
  33. for( String v: map.values()){
  34. System.out. println( "value="+v);
  35. }
  36. //4通过map.entrySet遍历key和value(推荐使用,特别是容量大时)
  37. System.out. println( "通过map.entrySet遍历key和value(推荐使用,特别是容量大时)");
  38. for( Map. Entry< String, String> entry: map.entrySet()){
  39. System.out. println( "key="+entry.getKey()+ "\tvalue="+entry.getValue());
  40. }
  41. }
  42. }

 --------------------测试结果-----------------------------------

有一个疑问,为什么遍历顺序为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="{&quot;mod&quot;:&quot;popu_824&quot;}"><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="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><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="{&quot;mod&quot;:&quot;popu_379&quot;}" 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="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    </article>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值