发布时间:2026/7/24 1:08:14
集合详解(五):集合嵌套与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/7/24 1:08:14

doom3 代码结构

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

2026/7/24 1:08:14

Java DDD(领域驱动设计)

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

2026/7/24 2:43:19

自动化发现系统约束框架设计:从原理到工程实践

这次我们来看一个关于自动化发现与约束框架的核心观点:没有一种通用的最优约束方案。这个主题探讨的是在人工智能和自动化系统中,如何设计有效的约束机制来引导发现过程,但不存在适用于所有场景的万能解决方案。从技术实践角度看,…

2026/7/24 2:43:19

Aeon.WorX通用对象生命周期管理:从状态机原理到Spring Boot实践

在制造业、软件开发和系统工程领域,管理一个对象从概念、设计、制造、运维到报废的全过程,一直是项目复杂度和数据一致性的挑战点。传统上,产品生命周期管理(PLM)和产品数据管理(PDM)系统试图解…

2026/7/24 2:43:19

多模态大模型实战16

第16章 多模态模型微调实战——LoRA/QLoRA/P-Tuning 学习目标 理解全参数微调 vs LoRA vs QLoRA vs P-Tuning的区别 掌握LoRA原理和参数配置 实战QLoRA微调VLM 用LLaMA-Factory微调LLaVA 16.1 微调策略对比 策略 可训练参数 显存 效果 适用场景 全参数微调 100% 最高 最好 数据…

2026/7/24 2:43:19

多模态大模型实战15

第15章 多模态安全与对齐——幻觉检测与红队测试 学习目标 理解VLM幻觉问题的类型和产生原因 掌握幻觉检测方法 了解多模态对齐技术(DPO/RLHF) 认识红队测试和安全评估 掌握防御策略 15.1 VLM幻觉问题 什么是VLM幻觉 VLM幻觉是指模型生成与图片内容不符的回答,包括"…

2026/7/24 2:43:19

Flash技术深度解析:从嵌入式存储到模型推理优化的完整指南

在深度学习模型推理和部署过程中,Flash 技术已经成为提升计算效率、降低延迟的关键手段。无论是处理大规模语言模型还是优化嵌入式设备的存储性能,理解 Flash 的工作原理和实际应用都至关重要。本文将以 Gemini 3.6 Flash 的改进为切入点,深入…

2026/7/24 2:38:18

学生党请注意!2026最新3款高口碑实用英语词汇学习软件推荐

核心要点: 1. 本次推荐全部基于2026年1-6月公立校实测使用数据,无任何商业合作;2. 重点拆解AI驱动类词汇工具的核心技术差异,避免盲目跟风选品;3. 针对不同学习场景给出适配建议,主打低时间成本提效。先聊聊…

2026/7/23 12:54:51

Unity与Python本地通信:基于Flask的跨语言数据交换实战

1. 项目概述:为什么我们需要一个本地通信服务器?在游戏开发、数字孪生、仿真训练等众多领域,Unity作为强大的实时3D内容创作平台,其核心逻辑通常由C#驱动。然而,当我们需要进行复杂的数据分析、机器学习推理、科学计算…

2026/7/24 0:03:10

不同品牌斜齿行星减速机如何替换?以PX与PAG系列为例

不同品牌斜齿行星减速机如何替换?以 PX 与 PAG 系列为例 一、系列对应不等于型号直接互换 PX 与 PAG 都属于斜齿、方法兰、输出轴式精密行星减速机,结构形式和应用方向具有对应关系。 原设备使用PX系列时,可以优先从PAG系列中寻找替换型号。但…

2026/7/24 0:03:10

jdk8 把list 扁平化成String 多个以逗号分隔

在 JDK 8 中&#xff0c;将 List 扁平化为以逗号分隔的 String&#xff0c;有几种非常简洁且高效的方法。&#x1f680; 推荐方案&#xff1a;使用 Collectors.joining()这是最标准的 Java 8 写法&#xff0c;适用于 List<String>。javaimport java.util.stream.Collecto…

2026/7/24 0:03:10

java 两个 long id 怎么合并成一个long id 并且不重复

“把两个 Long ID 合并成一个唯一的 Long ID&#xff0c;且保证不重复”这个需求&#xff0c;在 Java 里直接做数学上的“完美合并”是不可能的。因为两个 Long&#xff08;各 64 位&#xff09;要合并成一个 Long&#xff08;64 位&#xff09;&#xff0c;在信息论上是有损压…

2026/7/23 23:42:43

3个高效策略:快速掌握Axure中文界面配置

3个高效策略&#xff1a;快速掌握Axure中文界面配置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP的英文界面感…