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

本月热点