【我的Java笔记】集合的遍历嵌套
来源:互联网 发布:appaso优化方案 编辑:程序博客网 时间:2024/06/06 04:17
1.HashMap集合嵌套HashMap集合
例:
/* * 基础班: * 佩里西奇---20 * 坎德雷瓦---21 * 就业班: * 伊卡尔迪---22 * 汉达诺维奇---23 * * */import java.util.HashMap;import java.util.Set;public class HashMapDemo {public static void main(String[] args) {// 创建两个HashMap小集合分别存储“基础班”和“就业班”HashMap<String, Integer> hm1 = new HashMap<String, Integer>();hm1.put("佩里西奇", 20);hm1.put("坎德雷瓦", 21);HashMap<String, Integer> hm2 = new HashMap<String, Integer>();hm2.put("伊卡尔迪", 22);hm2.put("汉达诺维奇", 23);// 创建HashMap大集合分别储存"基础班"和"就业班"HashMap<String, HashMap<String, Integer>> hm = new HashMap<String, HashMap<String, Integer>>();hm.put("基础班", hm1);hm.put("就业班", hm2);// 遍历大集合Set<String> bigset = hm.keySet();for (String bigKey : bigset) {System.out.println(bigKey+":");// 拿出小集合HashMap<String, Integer> smallMap = hm.get(bigKey);// 遍历小集合Set<String> smallset = smallMap.keySet();for (String smallKey : smallset) {System.out.println("\t"+smallKey + "---" + smallMap.get(smallKey));}}}}
2.HashMap集合嵌套AraayList集合
例:
/* * 国际米兰: * 伊卡尔迪 * 汉达诺维奇 * 曼联: * 卢卡库 * 德赫亚 * 意大利: * 卡萨诺 * 布冯 * * */import java.util.ArrayList;import java.util.HashMap;import java.util.Set;public class HashMapDemo1 {public static void main(String[] args) {//创建三个ArrayList小集合并添加数据ArrayList<String> inter = new ArrayList<String>();ArrayList<String> mu = new ArrayList<String>();ArrayList<String> italy = new ArrayList<String>();inter.add("伊卡尔迪");inter.add("汉达诺维奇");mu.add("卢卡库");mu.add("德赫亚");italy.add("卡萨诺");italy.add("布冯");//创建HashMap大集合并储存三个小集合HashMap<String,ArrayList<String>> fc = new HashMap<String,ArrayList<String>>();fc.put("国际米兰",inter);fc.put("曼联",mu);fc.put("意大利",italy);//遍历大集合Set<String> big = fc.keySet();for(String bigKey:big) {System.out.println(bigKey+":");//获取小集合ArrayList<String> small = fc.get(bigKey);//遍历小集合for(String smallList:small) {System.out.println("\t"+smallList);}}}}
3.ArrayList集合嵌套HashMap集合
例:
/* * 国际米兰---AC米兰 * 尤文图斯---都灵 * * 曼联---曼城 * 阿森纳---热刺 * * 博卡青年---河床 * 费耶诺德---阿贾克斯 * */import java.util.ArrayList;import java.util.HashMap;import java.util.Set;public class HashMapDemo2 {public static void main(String[] args) {// 分别创建三个HashMap小集合并赋值HashMap<String, String> hm1 = new HashMap<String, String>();HashMap<String, String> hm2 = new HashMap<String, String>();HashMap<String, String> hm3 = new HashMap<String, String>();hm1.put("国际米兰", "AC米兰");hm1.put("尤文图斯", "都灵");hm2.put("曼联", "曼城");hm2.put("阿森纳", "热刺");hm3.put("博卡青年", "河床");hm3.put("费耶诺德", "阿贾克斯");// 创建ArrayList大集合并储存三个小集合ArrayList<HashMap<String, String>> big = new ArrayList<HashMap<String, String>>();big.add(hm1);big.add(hm2);big.add(hm3);// 遍历大集合for (HashMap<String, String> small : big) {// 遍历小集合Set<String> smallset = small.keySet();for (String key : smallset) {System.out.println(key + "---" + small.get(key));}System.out.println();}}}
阅读全文
0 0
- 【我的Java笔记】ArrayList集合的遍历嵌套
- 【我的Java笔记】集合的遍历嵌套
- 集合的嵌套遍历
- * 集合的嵌套遍历
- 集合框架-集合的嵌套遍历
- java的集合嵌套及三种遍历
- JAVA Map 遍历Map 集合 map 的嵌套
- 我的java笔记(集合)
- 集合框架-集合的嵌套遍历(HashMap嵌套HashMap)
- 集合框架-集合的嵌套遍历(HashMap嵌套ArrayList)
- 集合框架-集合的嵌套遍历(ArrayList嵌套HashMap)
- 集合框架-集合的嵌套遍历(多层嵌套)
- c标签遍历嵌套的List集合
- 双列集合Map的嵌套遍历
- JAVA集合-集合的遍历
- java集合的遍历
- c标签遍历集合嵌套的List集合
- 我的笔记:精通JAVA集合类
- Redis安装与实际应用
- 想学AI?没那么简单、但也没那么难!(内附技术图谱)
- 三极管开关电路
- 你也可以成为Web开发工程师!——Web开发工程师完全成长指南
- Maven 查看版本号
- 【我的Java笔记】集合的遍历嵌套
- mysql的net start /stop
- svn 遇到“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法
- appium命令
- 原生js实现轮播
- c++中初始化列表和构造函数初始化
- chrome postman插件安装
- kald脚本解释
- [Leetcode] 465. Optimal Account Balancing 解题报告