Map嵌套Map:

例:

  AAA:

    Javas班:

      001  熊大

      002  熊二

    Hdoop班

      001  小猪猪

      002  小菲菲

  ★使用增强for循环遍历Set数组:

import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry; public class MapDemo {
public static void main(String[] args) {
//定义javas班的集合
HashMap<String, String> javas=new HashMap<String,String>();
//定义Hdoop班集合
HashMap<String, String> Hdoop=new HashMap<String,String>();
//向班级存储学生
javas.put("001", "熊大");
javas.put("002", "熊二"); Hdoop.put("001", "小猪猪");
Hdoop.put("002", "小菲菲"); //定义AAA容器,键是班级的名字,值是两个班级的容器
HashMap<String, HashMap<String, String>> AAA=new HashMap<String, HashMap<String, String>>(); AAA.put("javas班", javas);
AAA.put("Hdoop班", Hdoop);
entrySet1(AAA);
} private static void entrySet1(HashMap<String, HashMap<String, String>> AAA) {
//调用集合AAA的方法,entrySet将AAA集合的键封装到Set集合中。
Set<Entry<String, HashMap<String, String>>> aaa=AAA.entrySet();
//增强for循环遍历set集合
for(Entry<String, HashMap<String, String>> p:aaa){
//System.out.println(p);
//getkey获得AAA的键,getValue获得值
String classNameKey=p.getKey();
HashMap<String, String> classMap =p.getValue();
System.out.println(classNameKey);
//System.out.println(classMap);
//将classMap装进Set集合
Set<Entry<String, String>> s=classMap.entrySet();
for (Entry<String, String> q:s) {
//getKey获得班级的键,getValue获得值
String numKey=q.getKey();
String nameValue=q.getValue();
System.out.println(numKey+": "+nameValue);
}
}
}
}

 ★ 使用迭代器遍历Set数组

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Set; //Map嵌套存储Map
public class MaoMapDemo {
public static void main(String[] args) {
//定义javas班的集合
HashMap<String, String> javas=new HashMap<String,String>();
//定义Hdoop班集合
HashMap<String, String> Hdoop=new HashMap<String,String>();
//向班级存储学生
javas.put("001", "熊大");
javas.put("002", "熊二"); Hdoop.put("001", "小猪猪");
Hdoop.put("002", "小菲菲"); //定义AAA容器,键是班级的名字,值是两个班级的容器
HashMap<String, HashMap<String, String>> AAA=new HashMap<String, HashMap<String, String>>(); AAA.put("javas班", javas);
AAA.put("Hdoop班", Hdoop);
entrySet1(AAA);
} private static void entrySet1(HashMap<String, HashMap<String, String>> AAA) {
//调用集合AAA的方法,entrySet将AAA集合的键封装到Set集合中。
Set<Entry<String, HashMap<String, String>>> classNameSet=AAA.entrySet();
/*迭代Set集合*/
//集合绑定迭代器
Iterator<Entry<String, HashMap<String, String>>> it=classNameSet.iterator();
while (it.hasNext()) {
//遍历集合
Entry<String, HashMap<String, String>> next= it.next();
//System.out.println(next);
//getkey获得键,getValue获得值
String classNameKey=next.getKey();
HashMap<String, String> classMap = next.getValue();
//AAA容器的键,班级名字classNameKey
System.out.println(classNameKey);
//AAA容器的值,班级所有元素classMap
//System.out.println(classMap); //entrySet将classMap集合的键封装到Set集合中。
Set<Entry<String, String>> studentSet=classMap.entrySet();
//迭代,集合绑定迭代器
Iterator<Entry<String, String>> studentIt=studentSet.iterator();
while (studentIt.hasNext()) {
//遍历集合
Entry<String, String> studentEntry = studentIt.next();
//getkey获得键,getValue获得值
String numKey=studentEntry.getKey();
String nameValue=studentEntry.getValue(); System.out.println(numKey+": "+nameValue);
}
} }
}

Map的嵌套使用的更多相关文章

  1. Map的嵌套 练习

    Map的嵌套   练习 利用迭代和增强for循环的两种方式实现如下效果 package cn.ccc; import java.util.HashMap;import java.util.Iterat ...

  2. 水果(map的嵌套)

    夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了 ...

  3. Map的嵌套,HDU(1263)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 新学的map的嵌套 #include <stdio.h> #include < ...

  4. 双列集合Map的嵌套遍历

    双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...

  5. map的嵌套 + 例题(水果)

    水果 http://acm.hdu.edu.cn/showproblem.php?pid=1263 Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~Joe经营着一个 ...

  6. Map接口----Map中嵌套Map

    package cn.good.com; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impo ...

  7. Map的嵌套

    package cn.lijun.demo2; import java.util.HashMap; import java.util.Iterator; import java.util.Set; p ...

  8. fastjson排序 Map多层嵌套转换自动排序问题终极解决方案

    阅读更多 最近项目中用到了fastjson(1.2.15)需要将前端多层嵌套json转换为map,由于map的无序性,想了很多办法,最终找到使用 Map m= JSONArray.parseObjec ...

  9. golang map多层嵌套使用及遍历方法汇总

    原文:https://blog.csdn.net/boyhandsome7/article/details/79734847 ------------------------------------- ...

随机推荐

  1. flask项目结构(三)使用蓝图

    简介: Flask中的蓝图旨在针对这些情况: 把一个应用分解成一系列的蓝图.对于大型的应用是理想化的:一个项目能实例化一个应用, 初始化一些扩展,以及注册一系列的蓝图. 以一个 URL 前缀和/或子域 ...

  2. 7系列FPGA远程更新方案-QuickBoot(转)

    reference: http://xilinx.eetrend.com/d6-xilinx/article/2014-04/7009.html reference :  quickboot meth ...

  3. [Paper] LCS: An Efficient Data Eviction Strategy for Spark

    Abstract Classical strategies do not aware of recovery cost, which could cause system performance de ...

  4. L293 给地球降温

    Countries look at ways to tinker with Earth’s thermostat The idea of cooling the climate with strato ...

  5. 压缩后的数据 要经过 base64_encode 后才能在网络上传送

    function ob_gzip($content) // $content 就是要压缩的页面内容{ if(!headers_sent() && // 如果页面头部信息还没有输出 ex ...

  6. springboot默认创建的bean是单实还是多例

    转:https://blog.csdn.net/q1512451239/article/details/53122687 springboot默认创建的bean是单实还是多例 曾经面试的时候有面试官问 ...

  7. SQL注入之Sqli-labs系列第十二关

    开始挑战第十二关(Error Based- Double quotes- String) 12点半了,不困,继续,继续,继续 先看看页面,通常的使用单引号等进行操作,看看啥么情况先 咦,出现错误信息了 ...

  8. linux 编译链接问题

    -rpath和-rpath-link 假设有3个文件,在同一目录下,有这样的依赖关系 test->liba.so->libd.so 如果编译test的时候这样写 gcc test.c –l ...

  9. Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎

    文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...

  10. 2.8 定位一组元素elements

    2.8 定位一组元素elements 前言    前面的几篇都是讲如何定位一个元素,有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象.webdriver 提 ...