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

相关新闻

AI证件照背景替换技术解析与应用实践

AI证件照背景替换技术解析与应用实践

1. 项目概述:证件照背景替换的痛点与解决方案每次需要提交证件照时最头疼的是什么?不是拍照本身,而是不同场合要求不同底色——求职简历要蓝底、护照申请要白底、考试报名又要求红底。传统解决方案要么反复跑照相馆重拍,要么自己用…

2026/7/24 1:05:48 阅读更多 →
论文降重与AIGC检测技术解析及工具应用

论文降重与AIGC检测技术解析及工具应用

1. 论文降重与AIGC检测的现状与挑战 写论文最头疼的莫过于查重和AIGC检测了。去年我指导的几个研究生,几乎人人都在为这个发愁——明明是自己写的论文,查重率却居高不下;而用AI辅助写作的内容,又总被检测系统标记为"非人类创…

2026/7/24 1:05:48 阅读更多 →
基于BERT的中文文本情感分类实战指南

基于BERT的中文文本情感分类实战指南

1. 项目概述中文文本情感分类是自然语言处理领域的基础任务之一,其目标是将给定的中文文本划分为积极、消极或中性等情感类别。随着预训练语言模型的兴起,基于BERT的文本分类方法已成为当前主流技术路线。本文将全面解析如何利用BERT预训练模型构建中文情…

2026/7/24 1:05:48 阅读更多 →

最新新闻

GPU故障排查三步法,送修前先做完这几步判断

GPU故障排查三步法,送修前先做完这几步判断

【文章概述】本文基于GPU运维一线经验,总结了硬件故障排查的三步判断逻辑——交叉验证、触发条件分析、外部干扰排除。适合服务器运维工程师、数据中心FAE、AI平台负责人参考。上篇已经梳理了GPU最常见的四类故障现象与排查方向:系统不识别、ECC报错、频…

2026/7/24 1:24:53 阅读更多 →
rawfile 资源与强类型词库加载器:schema / data / source 三层版本

rawfile 资源与强类型词库加载器:schema / data / source 三层版本

"开口练"的核心能力——本地检测填充词、犹豫词、笼统词——建立在三个 JSON 词库上:实时词库(16 填充 14 犹豫 20 笼统)、情感词库(146 词)、分层候选词库(9 组 97 条)。这批数据有…

2026/7/24 1:24:53 阅读更多 →
设备装不上、推行晃动、线缆缠绕?监护仪推车定制7大雷区与采购检查清单

设备装不上、推行晃动、线缆缠绕?监护仪推车定制7大雷区与采购检查清单

关键词: 监护仪推车;医疗设备定制;设备适配;医用推车;ICU设备 目录 设备兼容性:孔位公差0.5mm,差一点就是废铁承重设计:动态负载是静态的3-5倍,别只看标称值高度调节&a…

2026/7/24 1:24:53 阅读更多 →
AI决策可追溯性:技术实现与行业应用解析

AI决策可追溯性:技术实现与行业应用解析

1. AI决策可追溯性的行业痛点与核心价值在金融风控系统中,我们曾遇到一个典型案例:某AI信贷审批模型突然将一位优质客户的信用评分从850分骤降至620分,导致200万贷款申请被拒。技术团队花了整整三周时间逆向排查,才发现是某个特征…

2026/7/24 1:24:53 阅读更多 →
c端网页设计

c端网页设计

一个优秀的网站的主页应该怎么设计 在elementui当中,有这样一个概念 我们看一下csdn的主页结合这个案例 可以发现 主体页面框架的设计 大概就是这样 借用乔布斯的话 用户不知道他们想要什么,直到我们展示给他们看” 你观察得非常敏锐,抓住了国…

2026/7/24 1:24:53 阅读更多 →
LVDS接口PCB布局布线实战:从信号完整性基础到高速设计要点

LVDS接口PCB布局布线实战:从信号完整性基础到高速设计要点

1. 项目概述与核心挑战在高速数字电路设计的江湖里,信号完整性(SI)是决定一个系统成败的“内功心法”。我接触过不少项目,从早期的百兆以太网到如今动辄数Gbps的SerDes接口,一个深刻的体会是:原理图设计只是…

2026/7/24 1:23:53 阅读更多 →

日新闻

用Highcharts 创建可拖拽三维散点立方体3D图表

用Highcharts 创建可拖拽三维散点立方体3D图表

该案例基于Highcharts scatter3d 三维散点图实现空间立方体散点可视化,核心特色:三维 X/Y/Z 三轴空间,所有散点分布在 0~10 立方体空间内;散点使用径向渐变实现立体 3D 圆球质感;支持鼠标 / 触屏拖拽画布,…

2026/7/24 0:00:29 阅读更多 →
AppCertDlls:进程创建路径上的 DLL 入口

AppCertDlls:进程创建路径上的 DLL 入口

AppCertDlls:进程创建路径上的 DLL 入口 AppCertDlls 位于 HKLM\System\CurrentControlSet\Control\Session Manager\AppCertDlls。本文的程序功能是只读列出这个键在 64 位和 32 位注册表视图中的全部值,并显示每条值的来源、名称、类型和可安全显示的数…

2026/7/24 0:00:29 阅读更多 →
我的编程之路:第一篇博客

我的编程之路:第一篇博客

大家好,我是一名编程初学者,同时这也是我编程学习之路上的第一篇博客。在这里,我想要向大家介绍我的一些想法和规划。a.自我介绍我是一个刚刚接触编程的新手,目前在学习c语言,我对编程世界充满了强烈的好奇。当然&…

2026/7/24 0:00:29 阅读更多 →

周新闻

Go语言静态资源打包方案对比与实践指南

Go语言静态资源打包方案对比与实践指南

1. 项目背景与核心需求在Go语言开发中,我们经常需要处理静态资源文件的打包问题。无论是Web应用的模板文件、前端资源,还是配置文件、证书等,都需要随程序一起分发。传统做法是将这些文件与编译后的二进制文件放在同一目录下,但这…

2026/7/22 8:58:19 阅读更多 →
Go语言实现高性能LDAP认证服务的架构与实践

Go语言实现高性能LDAP认证服务的架构与实践

1. 项目背景与核心价值LDAP(轻量级目录访问协议)作为企业级身份认证的黄金标准,已经服务了超过80%的财富500强公司。我在金融科技领域实施统一认证体系时,发现传统Java方案存在启动慢、内存占用高等痛点。而Go语言凭借其协程并发模…

2026/7/24 1:23:39 阅读更多 →
【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

更多请点击: https://intelliparadigm.com 第一章:AI面试官实战指南的核心价值与适用场景 AI面试官并非替代人类HR的“黑箱工具”,而是以可解释、可审计、可迭代的方式,赋能招聘全链路的关键基础设施。其核心价值在于将主观经验沉…

2026/7/23 17:49:47 阅读更多 →

月新闻