码迷,mamicode.com
首页 > 其他好文 > 详细

Map的两种取出方式

时间:2018-06-18 21:01:55      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:entryset   TE   get   public   ring   put   while   static   map   

import java.util.*;

/*
* Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口。
* interface Map
* {
*    public static interface Entry
*   {
*     public abstract Object getKey();
*   }
* }

*/
public class Mapget {

  public static void main(String[] args)

  {

    Map<String,String> map=new HashMap<String,String>();
    map.put("02", "zhangsan2");
    map.put("03", "zhangsan3");
    map.put("01", "zhangsan1");
    map.put("04", "zhangsan4");

//第一种
    //先获取map集合的所有键的Set集合,keySet();
    Set<String> keySet=map.keySet();

    //有了Set集合,就可以获取其迭代器。

    Iterator<String> it=keySet.iterator();

    while(it.hasNext())

    {
      String key=it.next();
    //有了键就可以通过map集合的get方法获取对应的值。
      String value=map.get(key);
      System.out.println("key:"+key+"\tvalue:"+value);
    }

//第二种
    //获取map里面所有的映射关系。
    Set<Map.Entry<String, String>> keySet1=map.entrySet();
    Iterator<Map.Entry<String,String>> it1=keySet1.iterator();

    while(it1.hasNext())

    {
      Map.Entry<String,String> i=it1.next();
      String key1=i.getKey();
      String value1=i.getValue();
      System.out.println("key:"+key1+"\tvalue:"+value1);
    }
  }

}

Map的两种取出方式

标签:entryset   TE   get   public   ring   put   while   static   map   

原文地址:https://www.cnblogs.com/shenhengjia/p/9196038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!