
题⽬ 1学⽣信息管理核⼼考点对象封装、List 定制排序、Set 去重重写 equals/hashCode、数据清洗题⽬要求1.封装Student类学号id唯⼀标识、姓名、年龄、成绩2.向 List 中添加学⽣数据包含重复学号的⽆效数据3.对 List 做学号去重4.排序规则成绩降序 → 成绩相同则年龄升序5.输出最终清洗后的学⽣列表。打印结果如下去重排序后学⽣列表Student{idS001, 姓名张三, 年龄20, 成绩95.5}Student{idS003, 姓名王五, 年龄21, 成绩95.5}Student{idS002, 姓名李四, 年龄19, 成绩88.0}代码如下// 1. 封装 Student 类 public class Student { private String id; // 学号唯一标识用于去重 private String name; // 姓名 private int age; // 年龄 private double score; // 成绩 // 构造方法 public Student(String id, String name, int age, double score) { this.id id; this.name name; this.age age; this.score score; } // Getter Setter public String getId() { return id; } public String getName() { return name; } public int getAge() { return age; } public double getScore() { return score; } // 2. 重写 equals 和 hashCode**只根据学号 id 判断是否重复** Override public boolean equals(Object o) { if (this o) return true; if (o null || getClass() ! o.getClass()) return false; Student student (Student) o; return Objects.equals(id, student.id); // 学号相同即视为重复 } Override public int hashCode() { return Objects.hash(id); // 只根据学号生成哈希值 } // 重写 toString匹配输出格式 Override public String toString() { return Student{id id , 姓名 name , 年龄 age , 成绩 score }; } } // 测试主类 class StudentManager { public static void main(String[] args) { // 2. 向 List 添加学生数据包含重复学号的无效数据 ListStudent studentList new ArrayList(); studentList.add(new Student(S001, 张三, 20, 95.5)); studentList.add(new Student(S002, 李四, 19, 88.0)); studentList.add(new Student(S003, 王五, 21, 95.5)); studentList.add(new Student(S001, 张三, 20, 95.5)); // 重复学号无效数据 // 3. 学号去重利用 Set 自动去重依赖上面重写的 equals/hashCode SetStudent students new LinkedHashSet(studentList); ListStudent distinctList new ArrayList(students); // 4. 定制排序成绩降序 → 成绩相同则年龄升序 distinctList.sort((s1, s2) - { // 成绩降序分数大的在前 if (s2.getScore() ! s1.getScore()) { return Double.compare(s2.getScore(), s1.getScore()); } // 成绩相同年龄升序小的在前 return Integer.compare(s1.getAge(), s2.getAge()); }); // 5. 输出最终结果 System.out.println(去重排序后学生列表); for (Student s : distinctList) { System.out.println(s); } } }题⽬ 2部⻔员⼯管理核⼼考点类继承、Map 分组部⻔→员⼯列表、集合筛选、薪资统计题⽬要求11.⽗类Person姓名、年龄⼦类Employee继承并新增⼯号、部⻔、薪资2.将员⼯按部⻔分组存⼊MapString, ListEmployee3.统计每个部⻔的总薪资、平均薪资4.筛选出薪资8000 的员⼯5.查询指定部⻔技术部的所有员⼯。打印结果如下部⻔薪资统计部⻔技术部 | 总薪资21000.00 | 平均薪资10500.00部⻔⾏政部 | 总薪资6000.00 | 平均薪资6000.00部⻔市场部 | 总薪资17500.00 | 平均薪资8750.00薪资⼤于8000的员⼯员⼯{姓名张三, 部⻔技术部, 薪资9000.0}员⼯{姓名李四, 部⻔技术部, 薪资12000.0}员⼯{姓名赵六, 部⻔市场部, 薪资10000.0}技术部员⼯员⼯{姓名张三, 部⻔技术部, 薪资9000.0}员⼯{姓名李四, 部⻔技术部, 薪资12000.0}代码如下// 父类人 public class Person { // 属性姓名、年龄 protected String name; // protected子类可以直接用 protected int age; // 构造方法创建对象时给姓名、年龄赋值 public Person(String name, int age) { this.name name; // 给当前对象的name赋值 this.age age; // 给当前对象的age赋值 } // 获取姓名的方法getter public String getName() { return name; } // 获取年龄的方法getter public int getAge() { return age; } } // 子类员工 extends 继承 父类 Person class Employee extends Person { // 员工独有的属性 private String empId; // 工号 private String dept; // 部门 private double salary; // 薪资 // 构造方法创建员工对象时赋值 public Employee(String name, int age, String empId, String dept, double salary) { super(name, age); // 调用父类的构造方法给姓名、年龄赋值 this.empId empId; // 给自己的工号赋值 this.dept dept; // 给自己的部门赋值 this.salary salary; // 给自己的薪资赋值 } // 获取 工号 public String getEmpId() { return empId; } // 获取 部门 public String getDept() { return dept; } // 获取 薪资 public double getSalary() { return salary; } // 输出对象时显示这个格式 Override public String toString() { return 员工{姓名 name , 部门 dept , 薪资 salary }; } } // 测试主类 class EmpManager { public static void main(String[] args) { // ① 初始化员工数据 ListEmployee empList new ArrayList(); empList.add(new Employee(张三, 25, E001, 技术部, 9000.0)); empList.add(new Employee(李四, 26, E002, 技术部, 12000.0)); empList.add(new Employee(王五, 24, E003, 行政部, 6000.0)); empList.add(new Employee(赵六, 27, E004, 市场部, 10000.0)); empList.add(new Employee(孙七, 23, E005, 市场部, 7500.0)); // ② 按部门分组Map部门名, 该部门员工集合 MapString, ListEmployee deptEmpMap empList.stream() .collect(Collectors.groupingBy(Employee::getDept)); // ③ 统计每个部门总薪资、平均薪资 System.out.println(部门薪资统计); for (Map.EntryString, ListEmployee entry : deptEmpMap.entrySet()) { String dept entry.getKey(); ListEmployee emps entry.getValue(); double sum emps.stream().mapToDouble(Employee::getSalary).sum(); double avg sum / emps.size(); System.out.printf(部门%s | 总薪资%.2f | 平均薪资%.2f%n, dept, sum, avg); } // ④ 筛选薪资8000的员工 System.out.println(\n薪资大于8000的员工); ListEmployee highSalaryEmp empList.stream() .filter(e - e.getSalary() 8000) .collect(Collectors.toList()); highSalaryEmp.forEach(System.out::println); // ⑤ 查询技术部所有员工 System.out.println(\n技术部员工); ListEmployee techEmp deptEmpMap.get(技术部); techEmp.forEach(System.out::println); } }题⽬ 3图书管理核⼼考点TreeSet 定制排序、重写 equals/hashCode、Set 唯⼀校验、对象⾃然排序题⽬要求1.封装Book类ISBN唯⼀、书名、作者、价格2.ISBN 相同则视为同⼀本书Set ⾃动去重3.使⽤TreeSet存储排序规则价格升序 → 书名字典序4.验证重复 ISBN 的图书⽆法添加5.输出所有图书。打印结果如下图书列表去重排序Book{ISBN97803, 书名Spring实战, 价格89.0}Book{ISBN97802, 书名深⼊理解JVM, 价格89.0}Book{ISBN97801, 书名Java编程思想, 价格108.0}代码如下public class Book implements ComparableBook{ private String isbn; // 唯一标识用于去重 private String bookName; private String author; private double price; // 构造方法 public Book(String isbn, String bookName, String author, double price) { this.isbn isbn; this.bookName bookName; this.author author; this.price price; } // getter 方法 public String getIsbn() { return isbn; } public String getBookName() { return bookName; } public double getPrice() { return price; } // 2. 重写 equals hashCodeISBN相同则视为同一本书Set去重 Override public boolean equals(Object o) { if (this o) return true; if (o null || getClass() ! o.getClass()) return false; Book book (Book) o; return Objects.equals(isbn, book.isbn); // 只比较ISBN } Override public int hashCode() { return Objects.hash(isbn); // 只根据ISBN生成哈希 } // 3. 实现自然排序 Comparable价格升序 → 书名字典序 Override public int compareTo(Book other) { // ① 先按价格升序 if (this.price ! other.price) { return Double.compare(this.price, other.price); } // ② 价格相同按书名字典顺序升序 return this.bookName.compareTo(other.bookName); } // 重写toString匹配输出格式 Override public String toString() { return Book{ISBN isbn ,书名 bookName ,价格 price }; } } // 测试主类 class BookManager { public static void main(String[] args) { // 4. 使用 TreeSet 存储自带排序 去重 TreeSetBook bookSet new TreeSet(); // 添加图书包含重复ISBN的测试数据 bookSet.add(new Book(97801, Java编程思想, 埃克尔, 108.0)); bookSet.add(new Book(97802, 深入理解JVM, 周志明, 89.0)); bookSet.add(new Book(97803, Spring实战, Craig, 89.0)); bookSet.add(new Book(97801, Java编程思想, 埃克尔, 108.0)); // 重复ISBN无法添加 // 5. 输出所有图书 System.out.println(图书列表去重排序); for (Book book : bookSet) { System.out.println(book); } } }