尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
集合详解(五):集合嵌套与Collections工具类
一、集合嵌套1、HashMap嵌套HashMapspan stylefont-size:18px; /* * HashMap嵌套HashMap * * * * 先存储元素然后遍历元素 */ public void test3(){ // 创建集合对象 HashMapString, HashMapString, Integer czbkMap new HashMapString, HashMapString, Integer(); // 创建基础班集合对象 HashMapString, Integer jcMap new HashMapString, Integer(); // 添加元素 jcMap.put(陈玉楼, 20); jcMap.put(高跃, 22); // 把基础班添加到大集合 czbkMap.put(jc, jcMap); // 创建就业班集合对象 HashMapString, Integer jyMap new HashMapString, Integer(); // 添加元素 jyMap.put(李杰, 21); jyMap.put(曹石磊, 23); // 把基础班添加到大集合 czbkMap.put(jy, jyMap); //遍历集合 SetString czbkMapSet czbkMap.keySet(); for(String czbkMapKey : czbkMapSet){ System.out.println(czbkMapKey); HashMapString, Integer czbkMapValue czbkMap.get(czbkMapKey); SetString czbkMapValueSet czbkMapValue.keySet(); for(String czbkMapValueKey : czbkMapValueSet){ Integer czbkMapValueValue czbkMapValue.get(czbkMapValueKey); System.out.println(\tczbkMapValueKey---czbkMapValueValue); } } }/span2、HashMap集合的值是ArrayListspan stylefont-size:18px; /* *需求 *假设HashMap集合的元素是ArrayList。有3个。 *每一个ArrayList集合的值是字符串。 *元素我已经完成请遍历。 *结果 * 三国演义 * 吕布 * 周瑜 * 笑傲江湖 * 令狐冲 * 林平之 * 神雕侠侣 * 郭靖 * 杨过 */ public void test4(){ // 创建集合对象 HashMapString, ArrayListString hm new HashMapString, ArrayListString(); // 创建元素集合1 ArrayListString array1 new ArrayListString(); array1.add(吕布); array1.add(周瑜); hm.put(三国演义, array1); // 创建元素集合2 ArrayListString array2 new ArrayListString(); array2.add(令狐冲); array2.add(林平之); hm.put(笑傲江湖, array2); // 创建元素集合3 ArrayListString array3 new ArrayListString(); array3.add(郭靖); array3.add(杨过); hm.put(神雕侠侣, array3); //遍历集合 SetString set hm.keySet(); for(String key : set){ System.out.println(key); ArrayListString value hm.get(key); for(String s : value){ System.out.println(\ts); } } }/span3、ArrayList集合嵌套HashMapspan stylefont-size:18px; /* ArrayList集合嵌套HashMap集合并遍历。 需求 假设ArrayList集合的元素是HashMap。有3个。 每一个HashMap集合的键和值都是字符串。 元素我已经完成请遍历。 结果 周瑜---小乔 吕布---貂蝉 郭靖---黄蓉 杨过---小龙女 令狐冲---任盈盈 林平之---岳灵珊 */ public void test5(){ // 创建集合对象 ArrayListHashMapString, String array new ArrayListHashMapString, String(); // 创建元素1 HashMapString, String hm1 new HashMapString, String(); hm1.put(周瑜, 小乔); hm1.put(吕布, 貂蝉); // 把元素添加到array里面 array.add(hm1); // 创建元素1 HashMapString, String hm2 new HashMapString, String(); hm2.put(郭靖, 黄蓉); hm2.put(杨过, 小龙女); // 把元素添加到array里面 array.add(hm2); // 创建元素1 HashMapString, String hm3 new HashMapString, String(); hm3.put(令狐冲, 任盈盈); hm3.put(林平之, 岳灵珊); // 把元素添加到array里面 array.add(hm3); // 遍历 for (HashMapString, String hm : array) { SetString set hm.keySet(); for (String key : set) { String value hm.get(key); System.out.println(key --- value); } } } /span4、多层嵌套span stylefont-size:18px; /* * 为了更符合要求 * 这次的数据就看成是学生对象。 * */ public void test6(){ // 创建大集合 HashMapString, HashMapString, ArrayListStudent czbkMap new HashMapString, HashMapString, ArrayListStudent(); // 北京校区数据 HashMapString, ArrayListStudent bjCzbkMap new HashMapString, ArrayListStudent(); ArrayListStudent array1 new ArrayListStudent(); Student s1 new Student(林青霞, 27); Student s2 new Student(风清扬, 30); array1.add(s1); array1.add(s2); ArrayListStudent array2 new ArrayListStudent(); Student s3 new Student(赵雅芝, 28); Student s4 new Student(武鑫, 29); array2.add(s3); array2.add(s4); bjCzbkMap.put(基础班, array1); bjCzbkMap.put(就业班, array2); czbkMap.put(北京校区, bjCzbkMap); // 晚上可以自己练习一下 // 上海校区数据自己做 // 广州校区数据自己做 // 西安校区数据 HashMapString, ArrayListStudent xaCzbkMap new HashMapString, ArrayListStudent(); ArrayListStudent array3 new ArrayListStudent(); Student s5 new Student(范冰冰, 27); Student s6 new Student(刘意, 30); array3.add(s5); array3.add(s6); ArrayListStudent array4 new ArrayListStudent(); Student s7 new Student(李冰冰, 28); Student s8 new Student(张志豪, 29); array4.add(s7); array4.add(s8); xaCzbkMap.put(基础班, array3); xaCzbkMap.put(就业班, array4); czbkMap.put(西安校区, xaCzbkMap); // 遍历集合 SetString czbkMapSet czbkMap.keySet(); for (String czbkMapKey : czbkMapSet) { System.out.println(czbkMapKey); HashMapString, ArrayListStudent czbkMapValue czbkMap .get(czbkMapKey); SetString czbkMapValueSet czbkMapValue.keySet(); for (String czbkMapValueKey : czbkMapValueSet) { System.out.println(\t czbkMapValueKey); ArrayListStudent czbkMapValueValue czbkMapValue .get(czbkMapValueKey); for (Student s : czbkMapValueValue) { System.out.println(\t\t s.getName() --- s.getAge()); } } } }/span二、Collections工具类一概述1、Collections是针对集合进行操作的工具类都是静态方法。2、Collection和Collections的区别1Collection是单列集合的顶层接口有子接口List和Set。2Collections是针对集合操作的工具类有对集合进行排序和二分查找的方法二方法1、方法1public static T void sort(ListT list)排序默认情况下是自然顺序。2public static T int binarySearch(List? list,T key)二分查找3public static T T max(Collection? coll)最大值4public static void reverse(List? list)反转5public static void shuffle(List? list)随机置换2、实例1方法实例span stylefont-size:18px; /* * Collections:是针对集合进行操作的工具类都是静态方法。 * * 面试题 Collection和Collections的区别? Collection:是单列集合的顶层接口有子接口List和Set。 * Collections:是针对集合操作的工具类有对集合进行排序和二分查找的方法 * * 要知道的方法 public static T void sort(ListT list)排序 默认情况下是自然顺序。 public * static T int binarySearch(List? list,T key):二分查找 public static T T * max(Collection? coll):最大值 public static void reverse(List? list):反转 * public static void shuffle(List? list):随机置换 */ public void test1() { // 创建集合对象 ListInteger list new ArrayListInteger(); // 添加元素 list.add(30); list.add(20); list.add(50); list.add(10); list.add(40); System.out.println(list: list); // 排序 Collections.sort(list);// 排序 默认情况下是自然顺序。 System.out.println(list: list); // [10, 20, 30, 40, 50] // 二分查找 System.out.println(binarySearch: Collections.binarySearch(list, 30)); System.out.println(binarySearch: Collections.binarySearch(list, 300));// -6找不到即负的最大索引1 // 最值 System.out.println(max: Collections.max(list));// 50 System.out.println(max: Collections.min(list));// 10 // 反转逆序 Collections.reverse(list); System.out.println(list: list); // 随机置换扑克牌 Collections.shuffle(list); System.out.println(list: list); } /span2排序实例span stylefont-size:18px; /** * Collections可以针对ArrayList存储基本包装类的元素排序 * 存储自定义对象排序,需要实现Comparator接口或Comparable接口 */ public void test2() { // 创建集合对象 ListStudent list new ArrayListStudent(); // 创建学生对象 Student s1 new Student(林青霞, 27); Student s2 new Student(风清扬, 30); Student s3 new Student(刘晓曲, 28); Student s4 new Student(武鑫, 29); Student s5 new Student(林青霞, 27); // 添加元素对象 list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); // 排序 // 自然排序 // Collections.sort(list); // 比较器排序 // 如果同时有自然排序和比较器排序以比较器排序为主 Collections.sort(list, new ComparatorStudent() { Override public int compare(Student s1, Student s2) { int num s2.getAge() - s1.getAge(); int num2 num 0 ? s1.getName().compareTo(s2.getName()) : num; return num2; } }); // 遍历集合 for (Student s : list) { System.out.println(s.getName() --- s.getAge()); } } /span2模拟发牌方法一使用arraylistspan stylefont-size:18px; /* * 模拟斗地主洗牌和发牌 * * 分析 A:创建一个牌盒 B:装牌 C:洗牌 D:发牌 E:看牌 */ public void test3() { // 创建一个牌盒 ArrayListString array new ArrayListString(); // 装牌 // 黑桃A,黑桃2,黑桃3,...黑桃K // 红桃A,... // 梅花A,... // 方块A,... // 定义一个花色数组 String[] colors { ♠, ♥, ♣, ♦ }; // 定义一个点数数组 String[] numbers { A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K }; // 装牌 for (String color : colors) { for (String number : numbers) { array.add(color.concat(number)); } } array.add(小王); array.add(大王); // 洗牌 Collections.shuffle(array); // System.out.println(array: array); // 发牌 ArrayListString fengQingYang new ArrayListString(); ArrayListString linQingXia new ArrayListString(); ArrayListString liuYi new ArrayListString(); ArrayListString diPai new ArrayListString(); for (int x 0; x array.size(); x) { if (x array.size() - 3) { diPai.add(array.get(x));// 留三张底牌 } else if (x % 3 0) { fengQingYang.add(array.get(x)); } else if (x % 3 1) { linQingXia.add(array.get(x)); } else if (x % 3 2) { liuYi.add(array.get(x)); } } // 看牌 lookPoker(风清扬, fengQingYang); lookPoker(林青霞, linQingXia); lookPoker(刘意, liuYi); lookPoker(底牌, diPai); } public static void lookPoker(String name, ArrayListString array) { System.out.print(name 的牌是); for (String s : array) { System.out.print(s ); } System.out.println(); } /span方法二使用hashMap 和TreeSetspan stylefont-size:18px; /* * 思路 A:创建一个HashMap集合 * B:创建一个ArrayList集合 * C:创建花色数组和点数数组 * D:从0开始往HashMap里面存储编号并存储对应的牌 同时往ArrayList里面存储编号即可。 * E:洗牌(洗的是编号) * F:发牌(发的也是编号为了保证编号是排序的就创建TreeSet集合接收) * G:看牌(遍历TreeSet集合获取编号到HashMap集合找对应的牌) */ public void test4() { // 创建一个HashMap集合 HashMapInteger, String hm new HashMapInteger, String(); // 创建一个ArrayList集合 ArrayListInteger array new ArrayListInteger(); // 创建花色数组和点数数组 // 定义一个花色数组 String[] colors { ♠, ♥, ♣, ♦ }; // 定义一个点数数组 String[] numbers { 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A, 2, }; // 从0开始往HashMap里面存储编号并存储对应的牌,同时往ArrayList里面存储编号即可。 int index 0; for (String number : numbers) { for (String color : colors) { String poker color.concat(number); hm.put(index, poker); array.add(index); index; } } hm.put(index, 小王); array.add(index); index; hm.put(index, 大王); array.add(index); // 洗牌(洗的是编号) Collections.shuffle(array); // 发牌(发的也是编号为了保证编号是排序的就创建TreeSet集合接收) TreeSetInteger fengQingYang new TreeSetInteger(); TreeSetInteger linQingXia new TreeSetInteger(); TreeSetInteger liuYi new TreeSetInteger(); TreeSetInteger diPai new TreeSetInteger(); for (int x 0; x array.size(); x) { if (x array.size() - 3) { diPai.add(array.get(x)); } else if (x % 3 0) { fengQingYang.add(array.get(x)); } else if (x % 3 1) { linQingXia.add(array.get(x)); } else if (x % 3 2) { liuYi.add(array.get(x)); } } // 看牌(遍历TreeSet集合获取编号到HashMap集合找对应的牌) lookPoker(风清扬, fengQingYang, hm); lookPoker(林青霞, linQingXia, hm); lookPoker(刘意, liuYi, hm); lookPoker(底牌, diPai, hm); } // 写看牌的功能 public static void lookPoker(String name, TreeSetInteger ts, HashMapInteger, String hm) { System.out.print(name 的牌是); for (Integer key : ts) { String value hm.get(key); System.out.print(value ); } System.out.println(); } /span
RELATED

相关推荐

【OpenHarmony/HarmonyOS】ArkUI 科幻星空动效:Canvas 粒子、流星与声明式多层动画

【OpenHarmony/HarmonyOS】ArkUI 科幻星空动效:Canvas 粒子、流星与声明式多层动画

【OpenHarmony/HarmonyOS】ArkUI 科幻星空动效:Canvas 粒子、流星与声明式多层动画如何不依赖视频背景,在 HarmonyOS 页面中实现具有空间感的动态星空?本文拆解一个由 Canvas 星粒子、流星、渐变背景和 ArkUI 几何动画共同组成的可复用组件。…

📅 2026/9/12 6:15:23
doom3 代码结构

doom3 代码结构

doom3代码结构 code 原文地址 https://www.iddevnet.com/doom3/code.php , 原来 我认为这个网站已经倒闭了,毕竟从Doom4,quake4是2004,2005的, 关注的人太少了 ,我觉得这些文章有助于对于doom3,代码的理解&#xff…

📅 2026/9/16 11:02:21
2026亚太EMBA优势|主流院校中立择校测评

2026亚太EMBA优势|主流院校中立择校测评

民营企业家、企业创始人选EMBA,普遍纠结国际化适配、课程实用性、圈层质量与产业资源匹配度。本文从全球办学排名、院校办学定位、课程体系、学员圈层、产业资源五大核心维度,横向测评主流EMBA项目,深度拆解亚太EMBA优势。全文纯客观数据分析…

📅 2026/8/24 1:23:23
MORE NEWS

更多资讯

📰

ToastFish:用 Windows 通知栏背单词的免费开源工具

ToastFish:用 Windows 通知栏背单词的免费开源工具 【免费下载链接】ToastFish 一个利用摸鱼时间背单词的软件。 项目地址: https://gitcode.com/GitHub_Trending/to/ToastFish 上班、上课、午休,总有些碎片时间想记点单词,却又腾不出…

📰

Quartus Prime 18.1与ModelSim联合仿真全攻略:安装、授权到波形调试

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

Redis 未授权访问漏洞详解|漏洞复现 + 提权攻击 + 应急响应 + 全套安全加固

前言 Redis 是目前互联网使用最广泛的高性能内存数据库,常用于缓存、会话存储、热点数据存储。但绝大多数企业、运维人员因为默认配置不当,导致 Redis 暴露公网、无密码认证,产生高危 未授权访问漏洞。 攻击者可无需任何账号密码&#xff0…

📰

在 Elestio 上部署托管 FerretDB:五分钟搭建生产可用的 MongoDB 替代方案

后端数据库文档数据库 【免费下载链接】FerretDB A truly Open Source MongoDB alternative 项目地址: https://gitcode.com/gh_mirrors/fe/FerretDB 点击查看 免费下载 FerretDB 是一款开源、以 PostgreSQL(DocumentDB 扩展)为后端的 Mongo…

📰

pH 响应两亲嵌段高分子|PS-b-P4VP 聚苯乙烯‑b‑聚 4‑乙烯基吡啶 超分子组装、纳米催化模板基材

PS-b-P4VP(Polystyrene-block-Poly(4-vinylpyridine),聚苯乙烯-b-聚4-乙烯基吡啶)是一类由疏水性聚苯乙烯(PS)嵌段与弱碱性/亲水性聚4-乙烯基吡啶(P4VP)嵌段通过共价键连接而成的典型两亲性嵌段…

📰

408计算机考研资料这么多,到底该怎么排着用?

408计算机考研资料这么多,到底该怎么排着用? 【免费下载链接】cs-408 计算机考研专业课程408相关的复习经验,资源和OneNote笔记 项目地址: https://gitcode.com/GitHub_Trending/cs/cs-408 这篇文章带你过一遍开源仓库 cs-408 里的全部…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬