集合详解(五):集合嵌套与Collections工具类

发布时间:2026/9/15 12:28:56

集合详解(五):集合嵌套与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
延伸阅读

更多相关文章

2026/9/12 2:26:22

doom3 代码结构

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

2026/9/14 19:09:08

Java DDD(领域驱动设计)

一句话核心: DDD(领域驱动设计)不是一套框架,而是一种代码组织哲学。它要求你用代码直接翻译业务语言,而不是把业务逻辑散落在增删改查(CRUD)里。 对于Java开发者,最经典的DDD范式就…

2026/9/15 12:27:30

毕业设计级招聘系统拆解:Django+Vue前后端分离与权限设计

简介:基于PythonDjangoVueMySql开发的大学生就业招聘系统,是一份面向计算机专业毕业设计或前后端分离实战学习的完整资源包。系统覆盖管理员、企业、求职者三类角色,包含招聘信息管理、岗位申请、简历下载、在线留言、邀请面试等核心功能&…

2026/9/15 12:27:30

IAPP源码托管Trust Web实战:从PHP解析到部署避坑指南

简介:面向 PHP 与移动应用开发者的开源 APP 托管平台源码,基于 iAPP 框架构建,提供应用上传、版本管理、分发与权限控制等核心能力,适合需要快速搭建托管服务或学习 MVC 分层架构二次开发的读者。压缩包共 26 个文件,以…

2026/9/15 12:22:30

OpenCV 4.5.1接入wechat_qrcode:C++高鲁棒二维码识别编译实战

先说我自己的结论:在 OpenCV 4.5.1 里接入微信开源的二维码识别模块 wechat_qrcode,是 C 工程想要高鲁棒二维码识别的最短路径之一。这个模块来自 opencv_contrib,识别能力和 OpenCV 自带的 QRCodeDetector 完全不在一个量级。它内置了深度学…

2026/9/15 4:54:30

拯救者Y7000黑屏故障排查与维修实战指南

1. 项目概述:一台黑屏的拯救者Y7000,到底卡在哪一步? 联想拯救者Y7000系列笔记本,从2018年第一代搭载i5-8300H开始,到后来的i7-9750H、i7-10750H、i5-11400H,再到2023年款的R7-7840HS,它始终是学…

2026/9/15 0:01:16

AI英语单词APP开发:自适应学习算法与移动端优化实践

1. 项目概述 作为一名在移动应用开发领域摸爬滚打多年的老手,我最近完成了一个AI英语单词APP的开发项目。这个项目将传统单词记忆方法与现代AI技术相结合,打造了一款能够智能适应不同用户学习习惯的英语学习工具。 市面上大多数单词APP都存在一个通病&a…

2026/9/15 0:01:16

Flutter与OpenHarmony结合开发手语学习APP实战

1. 项目背景与核心价值作为一名同时接触过Flutter和OpenHarmony的开发者,最近我完成了一个基于Flutter for OpenHarmony的手语学习APP实战项目。这个项目最大的特点在于实现了跨平台框架与国产操作系统深度结合的创新实践——用Flutter开发的应用能完美运行在OpenHa…

2026/9/15 0:01:16

六个月成为机器人工程师:从ROS2到SLAM的实战路径

1. 六个月的紧迫感从哪来:先搞清楚你要成为哪种机器人工程师说实话,六个月的期限并不是一个宽松的时间线。市面上任何一本正经的机器人学教材都超过五百页,ROS2的官方文档可以翻到你怀疑人生,再加上ABB、KUKA这些工业机器人厂家动…

2026/9/14 11:59:31

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/14 13:53:59

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/15 11:42:23

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
咨询二维码