简体   繁体   English

EmployeeStore的SearchByEmail方法(HashMap)

[英]SearchByEmail method for EmployeeStore (HashMap)

Hey i have been looking around and i cant find a way to get a searchByEmail method to work. 嘿,我一直在四处寻找,我无法找到一种方法来使searchByEmail方法工作。 This application stores details for employee's in a company such as Name, id and e-mail. 此应用程序存储公司员工的详细信息,例如姓名,ID和电子邮件。 I need a method to search the hashmap by the e-mail addresses of its employees. 我需要一种方法来通过其员工的电子邮件地址搜索hashmap。

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

//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); or:
            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);
    }
    /*public void searchByName ()
    {
        //(for(Employee e : map.values()) {...}) 
        //and check for each employee if his/her email matches the searched value
        for(Employee e : map.values())
        {
            System.out.println(e);
            map.equals(getClass());

        }
    }*/
    public Employee searchByName(String name) 
    {
        Employee employee = map.get(name);    
        System.out.println(employee);
        return employee;
    }





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


}

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





}

//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.searchByName("James O' Carroll");
        //Store.print();

}
}

Well you could simply iterate over all the values: 那你可以简单地迭代所有的值:

public Employee searchByEmail(String email) 
{
    for (Employee employee : map.values() {
        if (email.equals(employee.getEmail()) {
            return employee;
        }
    }
    return null;
}

Caveats: 注意事项:

  • This is an O(N) operation 这是O(N)操作
  • There could be multiple matches - this only returns the first 可能有多个匹配 - 这只返回第一个匹配
  • You should probably validate that the email parameter is non-null 您应该验证email参数是否为非null

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

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