🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 集合框架 > 在集合框架中,主要分为 Collection 和 Map 两大接口。 > 在 Collection 中又分为 List (有序集合) 和 Set (无序集合)。List 和 Set 都是接口。 > Map 存储的 Key-Value 结构。 > 集合框架存储的元素可以是不定长的,即可以任意的添加,但是数组在定义的时候就确定了长度。实际开发中,对于同类型数据的结构,一般使用 Collection 结构会较多。 ## Collection ### List > 是实际开发中用的最多的一种数据结构,存储的单个元素,使用**泛型**去强制约束 List 中存放的是一致的数据类型 **插入元素** add() **遍历元素** - 通过元素索引下标方式 for 循环 - 通过 foreach 循环 - 通过迭代器 Iterator **删除元素** 如果通过遍历去查找相关元素进行删除的时候,不要使用 list.remove(int index) 该方法,因为使用该方法,会动态的直接改变集合的元素结构,导致遍历的不完整或者有错误。要使用迭代器遍历集合,调用 Iterator.remove() 方法删除。 ### Set 不详细阐述,在遍历的时候不能使用索引方式,只能通过迭代器和 foreach。 ## Map 类似的可以理解给集合元素中的值定义了一个 Key 键(遍历),之后可以通过相关的方法快速的定位到具体的 Value 中。 **获取某个 key 的 value** Value get(Key) **如何遍历 Map 结构** 获取 Map 的 Key 集合,通过遍历 Key 集合,获取 Value 值。 1. 获取 key 集合:map.keySet(); 2. 遍历 Key 集合:iterator; 3. 获取 Value 值:get(key); ## 开发注意事项 在实际开发中,List 一般使用 ArrayList 实现类去构建,Map 一般使用 HashMap 实现类去构建。 要掌握对于 List 和 Map 的添加元素、遍历元素。 要认识集合框架中关于对象在内存中的存储方法。 ## 使用 Collecionts 工具类 **使用 Collections.sort(list) 进行排序** 1. 集合中的元素类要实现 Comparable 接口,重写 compareTo 方法; 2. 调用 Collections.sort(list) 完成排序 ## 本节课的代码片段 **代码运行内存图** ![](https://box.kancloud.cn/31f0032935ff547f4e79ad3975896039_1979x849.png) Clazz.java ~~~ package com.ntqingniao.j96.bean; import java.util.List; public class Clazz { private String name; private String code; private List<Student> students; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } } ~~~ Clazz2.java ~~~ package com.ntqingniao.j96.bean; import java.util.Map; public class Clazz2 { private String name; private String code; private Map<String, Student> stus; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Map<String, Student> getStus() { return stus; } public void setStus(Map<String, Student> stus) { this.stus = stus; } } ~~~ Client.java ~~~ package com.ntqingniao.j96.bean; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Client { public static void main(String[] args) { Student pxj = new Student("彭贤君", "001"); Student hjn = new Student("黄佳男", "002"); Clazz j96 = new Clazz(); j96.setName("Java96班"); j96.setCode("java96"); // Step1:学生类型的集合定义完成了 List<Student> stus = new ArrayList<Student>(); // Step2: 添加数据 stus.add(pxj); stus.add(hjn); // Step3: 追加一个新的学生 Student ljn = new Student("陆佳楠", "003"); stus.add(ljn); // Step4: 设置班级学生属性 j96.setStudents(stus); // for (int i = 0; i < j96.getStudents().size(); i++) { // Student stu = j96.getStudents().get(i); // if (stu.getName().equals("黄佳男")) { // stu.setCode("004"); // } // } // for (int i = 0; i < j96.getStudents().size(); i++) { // // 打印对象,如果没有显式的调用toString()方法,编译器也会执行toString(); // System.out.println(j96.getStudents().get(i)); // } // // for (int i = 0; i < stus.size(); i++) { // System.out.println(stus.get(i)); // } // // for (Student stu : stus) { // System.out.println(stu); // } // // for (Student stu : stus) { // if (stu.getName().equals("黄佳男")) { // stu.setCode("005"); // } // } // for (int i = 0; i < j96.getStudents().size(); i++) { // Student stu = j96.getStudents().get(i); // System.out.println(stu.getName()); // if (stu.getName().equals("彭贤君")) { // j96.getStudents().remove(i); // 这样的实现是有问题的 // System.out.println(j96.getStudents().size()); // } // } // // for (Student stu : stus) { // System.out.println(stu); // } // 使用迭代器遍历 // Step1:创建迭代器 Iterator<Student> it = stus.iterator(); while (it.hasNext()) { Student stu = it.next(); System.out.println(stu); if (stu.getName().equals("黄佳男")) { it.remove(); } } System.out.println("处理完的结果======================"); it = stus.iterator(); while (it.hasNext()) { Student stu = it.next(); System.out.println(stu); } } } ~~~ Client2.java ~~~ package com.ntqingniao.j96.bean; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Client2 { public static void main(String[] args) { Student pxj = new Student("彭贤君", "001"); Student hjn = new Student("黄佳男", "002"); Clazz2 j96 = new Clazz2(); j96.setName("Java96班"); j96.setCode("java96"); Map<String, Student> stus = new HashMap<String, Student>(); stus.put("001", pxj); stus.put("002", hjn); j96.setStus(stus); Student stu = stus.get("001"); System.out.println(stu.getName()); System.out.println(stus.containsKey("003")); // 遍历Map // 第一步:获取Map中的Key集合 Set<String> keySet = stus.keySet(); // 第二步:迭代Key集合 Iterator<String> keyIt = keySet.iterator(); while (keyIt.hasNext()) { String key = keyIt.next(); // 根据Key获取Value Student value = stus.get(key); System.out.println(key + "=" + value); } } } ~~~ Client3.java ~~~ package com.ntqingniao.j96.bean; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Client3 { public static void main(String[] args) { Student pxj = new Student("彭贤君", "10", 20); Student hjn = new Student("黄佳男", "008",19); Student liuxiaowei = new Student("刘晓卫", "002",22); Student liuxiaowei2 = new Student("刘晓卫", "005",15); Student liuxiaowei3 = new Student("刘晓卫", "001",25); Student liuxiaowei4 = new Student("刘晓卫", "004",13); List<Student> stus = new ArrayList<Student>(); stus.add(pxj); stus.add(hjn); stus.add(liuxiaowei); stus.add(liuxiaowei2); stus.add(liuxiaowei3); stus.add(liuxiaowei4); Collections.sort(stus); for(Student stu : stus) { System.out.println(stu); } } } ~~~ Student.java ~~~ package com.ntqingniao.j96.bean; public class Student implements Comparable<Student>{ private String name; private String code; private int age; public Student(String name, String code) { super(); this.name = name; this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String name, String code, int age) { super(); this.name = name; this.code = code; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", code=" + code + ", age=" + age + "]"; } @Override public int compareTo(Student o) { if (this.age > o.age) { return 1; } else if (this.age == o.age) { return 0; } else { return -1; } } } ~~~ **扑克牌实例程序** > 由于出现花色特殊的字符,需要把Java文件的字符编码定义为 UTF-8 Constants.java ~~~ package com.ntqingniao.j96.bean; public class Constants { public static String[] FOLLOWS = {"♠","♥","♣","♦"}; public static int[] NUMBERS = {2,3,4,5,6,7,8,9,10,11,12,13,14}; public static String[] NUMBERS_STR = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; } ~~~ Poker.java ~~~ package com.ntqingniao.j96.bean; public class Poker { private String follow; private int number; public int compare(Poker p1, Poker p2) { return 0; } public Poker(String follow, int number) { super(); this.follow = follow; this.number = number; } public String getFollow() { return follow; } public void setFollow(String follow) { this.follow = follow; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return this.getFollow() + Constants.NUMBERS_STR[this.getNumber()-2]; } } ~~~ Client4.java ~~~ package com.ntqingniao.j96.bean; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Client4 { public static void main(String[] args) { List<Poker> pokers = new ArrayList<Poker>(); for (String follow : Constants.FOLLOWS) { for (int number : Constants.NUMBERS) { Poker poker = new Poker(follow, number); pokers.add(poker); } } int i = 0; for (Poker poker : pokers) { System.out.print(poker + " "); i++; if (i % 13 == 0) { System.out.println(); } } System.out.println("==========洗牌=========="); for (int j = 0; j < 5; j++) { System.out.println("===========第" + (j + 1) + "副牌=========="); Collections.shuffle(pokers); i = 0; for (Poker poker : pokers) { System.out.print(poker + " "); i++; if (i % 13 == 0) { System.out.println(); } } } } } ~~~