当前位置: 首页 > news >正文

⾯向对象和集合编程题

题⽬ 1:学⽣信息管理
核⼼考点:对象封装、List 定制排序、Set 去重(重写 equals/hashCode)、数据清洗
题⽬要求:
1.封装Student类:学号(id,唯⼀标识)、姓名、年龄、成绩;
2.向 List 中添加学⽣数据(包含重复学号的⽆效数据);
3.对 List 做学号去重;
4.排序规则:成绩降序 → 成绩相同则年龄升序;
5.输出最终清洗后的学⽣列表。
打印结果如下:
================去重+排序后学⽣列表================
Student{id='S001', 姓名='张三', 年龄=20, 成绩=95.5}
Student{id='S003', 姓名='王五', 年龄=21, 成绩=95.5}
Student{id='S002', 姓名='李四', 年龄=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 添加学生数据(包含重复学号的无效数据) List<Student> 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) Set<Student> students = new LinkedHashSet<>(studentList); List<Student> 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.将员⼯按部⻔分组存⼊Map<String, List<Employee>>
3.统计每个部⻔的总薪资、平均薪资;
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) { // ① 初始化员工数据 List<Employee> 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<部门名, 该部门员工集合> Map<String, List<Employee>> deptEmpMap = empList.stream() .collect(Collectors.groupingBy(Employee::getDept)); // ③ 统计每个部门总薪资、平均薪资 System.out.println("=====部门薪资统计===="); for (Map.Entry<String, List<Employee>> entry : deptEmpMap.entrySet()) { String dept = entry.getKey(); List<Employee> 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的员工===="); List<Employee> highSalaryEmp = empList.stream() .filter(e -> e.getSalary() > 8000) .collect(Collectors.toList()); highSalaryEmp.forEach(System.out::println); // ⑤ 查询技术部所有员工 System.out.println("\n=====技术部员工===="); List<Employee> 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{ISBN='97803', 书名='Spring实战', 价格=89.0}
Book{ISBN='97802', 书名='深⼊理解JVM', 价格=89.0}
Book{ISBN='97801', 书名='Java编程思想', 价格=108.0}

代码如下:

public class Book implements Comparable<Book>{ 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 & hashCode:ISBN相同则视为同一本书,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 存储(自带排序 + 去重) TreeSet<Book> 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); } } }
http://www.jsqmd.com/news/861375/

相关文章:

  • 在鸿蒙上跑一个端侧大模型——不用连云端数据全在本地
  • Java Comparator深度解析:从底层原理到实战应用
  • 内存管理与垃圾回收原理及机器学习实验研究
  • 一篇文章讲清楚—Windows 电脑中 CMD 和 PowerShell 有啥区别
  • 从CRUD到AI大神:小白程序员5个月逆袭之路(收藏版)
  • WorkBuddy:一个面向内容创作的桌面自动化助手实践
  • 1794-ACN15适配器模块
  • ComfyUI v0.22.0 更新:工作流模板升级、音频与多模态增强、OpenAPI 文档完善、节点能力大幅扩展
  • 2026年4月制冷厂推荐:制冷机组、制冷设备品牌、南宁制冷、反应釜制冷设备、商丘制冷、太原制冷、安徽冷水机、安徽制冷选择指南 - 优质品牌商家
  • Agent 认知破局:从具象表象到交互本质
  • EPRO MMS6120振动检测模块
  • 2026丛林穿越厂家怎么选:户外丛林穿越厂家、无动力乐园规划设计、无动力游乐设备非标定制、游乐场无动力游乐设备选择指南 - 优质品牌商家
  • 抖音获客失效?拆解本地商家流量困局的底层逻辑与破局路径
  • Linux 硬盘分区管理
  • 高性价比塑料链板输送机厂家排行适配指南
  • c语言中语句分类
  • Chiplet经济学:成本如何影响芯片产业发展?
  • 护照阅读器在海外的经典案例分享
  • fastapi · FastAPI framework, high performance, easy to learn, fast to code, ready for production
  • 鸿蒙PC的包管理工具 Homebrew 正式上线,Harmonybrew介绍及使用指南
  • 1987年5月15日中午11-13点出生性格、运势和命运
  • 从零开始学AI Agent:软件工程视角下的企业数字化转型实践指南(收藏版)
  • HBase 分布式集群部署实战:从解压到启动的完整指南
  • 健身 Agent:不止视频,更有 AI 人物实时跟练交互
  • 分享高三模拟卷资源盘点
  • 面试必看!大模型高频考点全覆盖(含LoRA、DPO、MoE、ZeRO、KV Cache等核心问题)
  • ZFX山海证券:“消费转向考验零售韧性”
  • 离散几何拓扑数论(终稿·全定义完整版一)
  • 网卡服务与配置
  • 2026年WMS软件怎么选?10款主流WMS软件功能对比与避坑指南