Employee类需含id、name、department、salary四字段,id为唯一主键;应使用ConcurrentHashMap替代ArrayList以实现O(1)查找;addEmployee须校验空值与重复id并抛异常;findById为常数时间,searchByName为O(n);删改操作需避免ConcurrentModificationException,多线程推荐ConcurrentHashMap。

Employee 类设计必须包含 id、name、department、salary 四个核心字段,且 id 应为唯一主键(建议用 Long 或 String,避免 int 溢出或重置问题)
很多初学者直接用 ArrayList 存所有员工,但没考虑 id 冲突和查找效率——增删查改都得遍历,1000 条数据时 getById() 平均要比较 500 次。真正在意响应速度,就该用 HashMap,以 id 为 key,O(1) 定位
增:addEmployee() 要校验空值和重复 id
不能只做 list.add() 就完事。常见错误是忽略 name 为空字符串、salary 、id 已存在等边界情况
-
id为null或已存在时,应抛出IllegalArgumentException,而不是静默失败 - 建议在
addEmployee()开头加Objects.requireNonNull(employee, "employee cannot be null") - 若用
HashMap,直接调用map.putIfAbsent(id, employee) == null判断是否新增成功
查:findById() 和 searchByName() 的性能差异很大
findById() 用 HashMap 是常数时间;但 searchByName(String keyword) 必须遍历全量数据,无法避免 O(n)。别试图给 name 建哈希索引——值不唯一,意义不大
25175企业客户管理系统能够方便的录入新的信息例如新的项目、新增客户等同时能够清楚的管理一些款项结算。由于功能的强大,用户可以在该系统上发表文章后让员工递交工作计划或工作报告。加强企业工作效率,为企业提高实力。因为该系统可由客户、程序员、负责人等身份登入 所以适合广大企业使用。管理首页:{信息录入|信息修改|信息查询}1信息录入:你可以根据实际情况进行以下操作 1-1新的项目
- 如需模糊搜索,用
stream().filter(e -> e.getName().contains(keyword))即可,别提前引入 Lucene 或数据库 - 若 name 查询频繁且数据量超 5000 条,才考虑用
ConcurrentHashMap+ 预建Map倒排索引(但会显著增加内存和维护成本)>
删与改:避免 ConcurrentModificationException
用 for (Employee e : list) 遍历时调 list.remove() 会抛异常。即使改成 Iterator,多线程下仍不安全
立即学习“Java免费学习笔记(深入)”;
- 单线程场景:用
Iterator.remove()或收集待删 id 后批量removeAll() - 多线程场景:别用
ArrayList或普通HashMap,换成CopyOnWriteArrayList(适合读多写少)或ConcurrentHashMap(推荐) -
updateEmployee()必须先get()再put(),不能直接改对象字段——否则缓存、日志、监听器可能拿不到变更前状态
public class EmployeeService {
private final Map storage = new ConcurrentHashMap<>();
public void addEmployee(Employee emp) {
if (emp == null || emp.getId() == null) {
throw new IllegalArgumentException("id and employee must not be null");
}
if (storage.putIfAbsent(emp.getId(), emp) != null) {
throw new IllegalArgumentException("duplicate id: " + emp.getId());
}
}
public Employee findById(Long id) {
return storage.get(id);
}
public List searchByName(String keyword) {
return storage.values().stream()
.filter(e -> e.getName() != null && e.getName().contains(keyword))
.collect(Collectors.toList());
}
}
真实项目里,storage 很快会从内存换成数据库,但上面这些校验逻辑、空值处理、并发策略,在 DAO 层依然要保留——不是换了个存储方式,就能把业务规则甩给 MySQL。









