思路:

1、定义集合

2、存储数据

3、添加元素

4、遍历

  4.1将需要遍历的集合的键封装到set集合中(这用到了entrySet方法,和Entry对象)

  4.2声明迭代器或者用for增强循环

  4.3通过set集合的getKey和getValue方法拿到值和键

5、打印输出就好

代码呈现如下:

  1. package com.aaa.demo;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map.Entry;
  6. import java.util.Set;
  7.  
  8. public class MaoMapDemo {
  9. public static void main(String[] args) {
  10. //定义Java班的集合
  11. HashMap<String, String> javas=new HashMap<String,String>();
  12. //定义Hdoop班的集合
  13. HashMap<String, String> Hdoop=new HashMap<String,String>();
  14. //向班级存储学生
  15. javas.put("001", "大冰");
  16. javas.put("002", "北岛");
  17.  
  18. Hdoop.put("001", "舒婷");
  19. Hdoop.put("002", "食指");
  20. //定义aaa容器,键是班级的名字 值是两个班级的容器
  21. HashMap<String, HashMap<String, String>> aaa=new HashMap<String,HashMap<String,String>>();
  22. aaa.put("现代", javas);
  23. aaa.put("近代", Hdoop);
  24. entrySet1(aaa);
  25. }
  26. public static void entrySet(HashMap<String,HashMap<String,String>> aaa){
  27. //调用集合aaa的方法 entrySet将aaa集合的键封装到Set集合中
  28. Set<Entry<String,HashMap<String,String>>> classNameSet=aaa.entrySet();
  29. //迭代Set集合
  30. Iterator<Entry<String,HashMap<String,String>>> it=classNameSet.iterator();
  31. while(it.hasNext()){
  32. Entry<String,HashMap<String,String>> next=it.next();
  33. //getKey getValue 得到键和值
  34. String classNameKey = next.getKey();
  35. System.out.println(classNameKey);
  36. //aaa容器的键 班级姓名classMap
  37. HashMap<String,String> classMap=next.getValue();
  38. //entrySet将classMap集合的键封装到set集合中
  39. Set<Entry<String,String>> studentSet=classMap.entrySet();
  40. //迭代
  41. Iterator<Entry<String, String>> studentIt = studentSet.iterator();
  42. while(studentIt.hasNext()){
  43. //遍历集合
  44. Entry<String,String> studentEntry=studentIt.next();
  45. String numKey = studentEntry.getKey();
  46. String nameValue = studentEntry.getValue();
  47. System.out.println(numKey+": "+nameValue);
  48. }
  49. }
  50. }
  51. public static void entrySet1(HashMap<String,HashMap<String,String>> aaa){
  52. //调用集合aaa的方法 entrySet将aaa集合的键封装到Set集合中
  53. Set<Entry<String,HashMap<String,String>>> cNS=aaa.entrySet();
  54. //for循环遍历得到班级
  55. for(Entry<String,HashMap<String,String>> next:cNS){
  56. //getKey getValue 得到键和值
  57. String classKey = next.getKey();
  58. //aaa容器的键 班级姓名classMap
  59. HashMap<String, String> classValue = next.getValue();
  60. //entrySet将classMap集合的键封装到set集合中
  61. Set<Entry<String, String>> studentSet = classValue.entrySet();
  62. System.out.println(classKey);
  63. //for循环遍历
  64. //Set<Entry<String, String>> entrySet = classValue.entrySet();
  65. for(Entry<String, String> studentEntry:studentSet){
  66. String numKey = studentEntry.getKey();
  67. String nameValue = studentEntry.getValue();
  68. System.out.println(numKey+": "+nameValue);
  69. }
  70. }
  71. }
  72. }

Java的map键值对的用法,map的遍历,Entry对象的使用的更多相关文章

  1. java 把json对象中转成map键值对

    相关:Json对象与Json字符串的转化.JSON字符串与Java对象的转换 本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据 比如json数据如下: {responseHeade ...

  2. 枚举类返回Map键值对,绑定到下拉框

    有时候,页面的下拉框要显示键值对,但是不想从数据库取,此时我们可以写一个枚举类, Java后台代码 1.枚举类 import java.util.HashMap; import java.util.M ...

  3. 用字典给Model赋值并支持map键值替换

    用字典给Model赋值并支持map键值替换 这个是昨天教程的升级版本,支持键值的map替换. 源码如下: NSObject+Properties.h 与 NSObject+Properties.m / ...

  4. 如何将Map键值的下划线转为驼峰

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:如何将Map键值的下划线转为驼峰: 例,将HashMap实例extMap键值下划线转为驼峰: 代码: HashMap<String ...

  5. Java Map 键值对排序 按key排序和按Value排序

    一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...

  6. 通过反射,获取linkedHashMap的最后一个键值对。对map按照值进行排序。

    1:通过反射,获取linkedHashMap的最后一个键值对. Map<Integer, Integer> map = new LinkedHashMap<>(); Field ...

  7. 根据map键值对,生成update与select语句,单条执行语句

    方法 constructUpdateSQL private static String constructUpdateSQL(String tableName, List<Map<Stri ...

  8. Java的HashMap键值对存储结构解析

    容器总体结构 Map存储键值对的数据结构是“数组+链表”的结构,结合了数组查询数据快和链表增删数据快的优点:用Entry[]存储键值对,Entry为类类型,类里面有四个属性:hash.K.V.next ...

  9. a=av###b=bv###c=cv map键值对 (a,av) (b,bv) (c,cv)

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; impo ...

随机推荐

  1. 《机器学习实战》Logistic回归

    注释:Ng的视频有完整的推到步骤,不过理论和实践还是有很大差别的,代码实现还得完成 1.Logistic回归理论 http://www.cnblogs.com/wjy-lulu/p/7759515.h ...

  2. 代码:PC 链接列表面板border的一种做法(每行之间有分割线)

    PC 链接列表面板,border的一种做法 做页面经常遇到一种问题,上面是标题,下面是单行链接列表.为了保证后台套页面方便,所有列表项必须完全一样.但我们无法解决第一行或最后一行多出来的分割线. 使用 ...

  3. Flex4学习笔记1---基本语法

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  4. linux 下使用 curl post

    命令: curl -X POST -d @/etc/lazada/lazada_tracking.txt   http://localhost:8080/booking/rs/LazadaServic ...

  5. leetcode1023

    class Solution(object): def getGroup(self,que): group = list() temp = '' for i in range(len(que)): c ...

  6. js 监听组合键盘事件

    有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 我们所熟悉的按键有这么集中类型: 单独的按键操作,如:delete.up.down等 两位 ...

  7. VC编译错误,把类误认为是函数

    这段代码是在一个动态库中,我像把这个类导出,于是加上 SC_EXPORTS 宏.class SC_EXPORTS CProtocolCheck{public: CProtocolCheck(void) ...

  8. VS2003在解决方案范围内搜索卡死问题的解决

    在Win7系统上使用VS2003的时候,在解决方案范围内搜索某个内容的时候,VS会卡死. 这是一个兼容性问题,Win7系统对VS2003的兼容性不好, 网上有人讲了一种解决方法是: 在vs2003的图 ...

  9. HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList的底层实现

    HashMap:底层是一个数组+链表实现 LinkedHashMap:底层是Hash表和链表的实现 ConcurrentHashMap:基于双数组和链表的Map接口的同步实现 ArrayList:底层 ...

  10. 腾讯云Linux VPS新硬盘分区与挂载教程(面板重装不丢失数据)

    以腾讯云Centos系统服务器为例,小记的是数据盘不在本地,大小为20G,以下的教程来自小夕博客的一篇相关添加教程的修改,适合腾讯云Linux Centos系统.说明:参数也许不对,我没有截图了,但所 ...