利用JAVA知识,实现对职工信息的输出,请各位会的帮帮忙,谢谢啦!
1条回答 默认 最新
- threenewbee 2023-04-21 22:21关注
Employee 类
public class Employee { private int id; private String name; private char sex; private double salary; public Employee(int id, String name, char sex, double salary) { this.id = id; this.name = name; this.sex = sex; this.salary = salary; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public char getSex() { return this.sex; } public void setSex(char sex) { this.sex = sex; } public double getSalary() { return this.salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", sex=" + sex + ", salary=" + salary + '}'; } }
主程序
import java.util.*; public class Main { public static void main(String[] args) { // 定义3个Employee对象 Employee employee1 = new Employee(1, "Tom", 'M', 5000.0); Employee employee2 = new Employee(2, "Jerry", 'M', 6000.0); Employee employee3 = new Employee(3, "Lucy", 'F', 7000.0); // 用Map存储Employee对象 Map<Integer, Employee> employeeMap = new HashMap<>(); employeeMap.put(employee1.getId(), employee1); employeeMap.put(employee2.getId(), employee2); employeeMap.put(employee3.getId(), employee3); // 用List存储Employee对象 List<Employee> employeeList = new ArrayList<>(); employeeList.add(employee1); employeeList.add(employee2); employeeList.add(employee3); // 用Set存储Employee对象 Set<Employee> employeeSet = new HashSet<>(); employeeSet.add(employee1); employeeSet.add(employee2); employeeSet.add(employee3); // 增加员工 Employee employee4 = new Employee(4, "Kate", 'F', 8000.0); employeeMap.put(employee4.getId(), employee4); employeeList.add(employee4); employeeSet.add(employee4); // 删除员工 int deleteEmployeeId = 2; employeeMap.remove(deleteEmployeeId); employeeList.removeIf(e -> e.getId() == deleteEmployeeId); employeeSet.removeIf(e -> e.getId() == deleteEmployeeId); // 修改员工 int updateEmployeeId = 3; Employee updatedEmployee = employeeMap.get(updateEmployeeId); updatedEmployee.setName("Lily"); employeeList.stream() .filter(e -> e.getId() == updateEmployeeId) .findFirst() .ifPresent(e -> e.setName("Lily")); employeeSet.stream() .filter(e -> e.getId() == updateEmployeeId) .findFirst() .ifPresent(e -> e.setName("Lily")); // 查询员工 int queryEmployeeId = 1; Employee queryEmployee = employeeMap.get(queryEmployeeId); System.out.println("Query result of Map: " + queryEmployee); employeeList.stream() .filter(e -> e.getId() == queryEmployeeId) .findFirst() .ifPresent(e -> System.out.println("Query result of List: " + e)); employeeSet.stream() .filter(e -> e.getId() == queryEmployeeId) .findFirst() .ifPresent(e -> System.out.println("Query result of Set: " + e)); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 求京东批量付款能替代天诚
- ¥15 slaris 系统断电后,重新开机后一直自动重启
- ¥15 51寻迹小车定点寻迹
- ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
- ¥15 关于vue2中methods使用call修改this指向的问题
- ¥15 idea自动补全键位冲突
- ¥15 请教一下写代码,代码好难
- ¥15 iis10中如何阻止别人网站重定向到我的网站
- ¥15 滑块验证码移动速度不一致问题
- ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含