gpt4 book ai didi

java - EmployeeStore 的搜索方法。使用HashMap

转载 作者:行者123 更新时间:2023-12-02 07:43:22 26 4
gpt4 key购买 nike

我目前正在申请存储员工详细信息,例如姓名、ID 和电子邮件地址。我正在使用 HashMap 来完成此操作。我目前在 searchByName、id 和电子邮件地址方法方面遇到困难。我该如何写一个?

这是我的代码:

//Imports.
import java.util.Scanner;
//********************************************************************
public class MainApp
{
private static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)
{
new MainApp().start();

}
public void start()
{
EmployeeStore Store = new EmployeeStore();
Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));

Store.add(new Employee ("Andy Carroll", 1171,"yahoo.com"));

Store.add(new Employee ("Luis Suarez", 7,"gmail.com"));
Store.print();
}
}

//Imports.
import java.util.HashMap;
//********************************************************************
public class EmployeeStore
{
HashMap<String, Employee> map;

//Constructor.
public EmployeeStore()
{
map = new HashMap<String,Employee>();
}
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
public void add(Employee employee)
{

map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
//Remove from the Hashmap : Employee.
public void remove(String key)
{
//Remove the Employee by name.
map.remove(key);
}
//********************************************************************
//Clear the Hashmap : Employee.
public void clear()
{
map.clear();
}
//********************************************************************
//Print the Hashmap : Employee.
public void print()
{
System.out.println("\n********Employee's in the Company.********");
for (Employee employee : map.values())
{
System.out.println("Employee Name:\t" + employee.getEmployeeName());
System.out.println("Employee Id:\t" + employee.getEmployeeId());
System.out.println("E-mail:\t"+ employee.getEmployeeEmail());
}

}
public Employee get(String name){
return map.get(name);
}


//********************************************************************
//********************************************************************


}

//Imports:

//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
private String employeeName;
private int employeeId;
private String employeeEmail;
//********************************************************************
//Constructor.
public Employee(String employeeName, int employeeId, String employeeEmail)
{
this.employeeName = employeeName;
this.employeeId = employeeId;
this.employeeEmail = employeeEmail;
}
//********************************************************************
//Getters.
public String getEmployeeEmail() {
return employeeEmail;
}
public void setEmployeeEmail(String employeeEmail) {
this.employeeEmail = employeeEmail;
}
public String getEmployeeName() {
return employeeName;
}
public int getEmployeeId() {
return employeeId;
}
//********************************************************************
//toString method.
public String toString() {
return "Employee [employeeName=" + employeeName + ", employeeId="
+ employeeId + ", employeeEmail=" + employeeEmail + "]";
}
//********************************************************************





}

最佳答案

在您的 EmployeeStore 中,您可以添加您提到的各种方法:

  1. searchByName 很容易实现,因为这是 map 的关键,所以一个简单的 map.get(name)应该可以解决问题
  2. 对于另外 2 个,您有 2 个选择:
    • 为每种类型的搜索创建一个映射(因此您将并行维护 3 个映射,一个的键是名称,一个的键是电子邮件,一个的键是 ID)。每张 map 将包含相同的员工。您可以像 1 中那样实现搜索。这更快,但使用更多内存
    • 循环遍历 map ( for(Employee e : map.values()) {...} ) 的值并检查每个员工的电子邮件是否与搜索值匹配

关于java - EmployeeStore 的搜索方法。使用HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11274868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com