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

map集合中的键值对对象遍历

时间:2018-05-13 23:44:49      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:string   try   str   port   一个   sys   oid   public   Map集合   

package com.day15.Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
* map集合中的第二种遍历方式
*/
public class MapFour {

  public static void main(String[] args) {
    Map<String, Integer> ma=new HashMap<>();
    ma.put("Kobe",20);
    ma.put("KG",21);
    ma.put("PP",22);
    ma.put("Allen",23);
    //Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中
    Set<Map.Entry<String,Integer>> es=ma.entrySet();
    //获取每一个对象
    Iterator<Map.Entry<String,Integer>> it=es.iterator();
    while(it.hasNext()) {
      Map.Entry<String, Integer> en=it.next();
      String key=en.getKey();
      Integer value=en.getValue();
      System.out.print(key+"="+value);//PP=22Kobe=20KG=21Allen=23
    }
  }

}

map集合中的键值对对象遍历

标签:string   try   str   port   一个   sys   oid   public   Map集合   

原文地址:https://www.cnblogs.com/zhujialei123/p/9033893.html

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