简体   繁体   English

EmployeeStore的搜索方法。 使用HashMap

[英]Search Method for an EmployeeStore. Using HashMap

I am currently making an application to store employee details such as name, id and email address. 我目前正在申请存储员工详细信息,例如姓名,身份证和电子邮件地址。 I am doing this using a HashMap. 我正在使用HashMap这样做。 I am currently having difficulty with a searchByName,id and email address methods. 我目前在使用searchByName,id和电子邮件地址方法时遇到困难。 How would i go about writing one ? 我怎么去写一个?

Here is my code: 这是我的代码:

//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 + "]";
    }
//********************************************************************





}

In your EmployeeStore, you can add the various methods you mention: 在您的EmployeeStore中,您可以添加您提到的各种方法:

  1. searchByName is easy to implement because that's the key for your map, so a simple map.get(name) should do the trick searchByName很容易实现,因为这是你的地图的关键,所以一个简单的map.get(name)应该做的伎俩
  2. for the other 2, you have 2 options: 对于其他2,您有2个选择:
    • create one map for each type of search (so you will maintain 3 maps in parallel, one where the key is the name, one where the key is the email and one where the key is the id). 为每种类型的搜索创建一个映射(因此您将并行维护3个映射,其中一个键是名称,一个键是电子邮件,另一个键是id)。 Each map will contain the same employees. 每张地图都包含相同的员工。 And you can implement the search like in 1. This is faster but uses more memory 并且您可以像1中那样实现搜索。这样更快但使用更多内存
    • loop over the values of the map ( for(Employee e : map.values()) {...} ) and check for each employee if his/her email matches the searched value 循环遍历地图的值( for(Employee e : map.values()) {...} )并检查每个员工他/她的电子邮件是否与搜索到的值匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM