2

我需要一个哈希图的搜索方法,但我不知道该怎么做。我也在尝试做一个编辑方法,我认为为此我需要这个方法。我的 hashMap 是用来存储员工数据的。我有MainApp, Employee 类和一个EmployeeStore类。任何人都可以帮忙吗?

public class MainApp
{

    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();
        Store.clear();
        Store.print();

        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();
        Store.remove("Andy Carroll");
        Store.print();


    }

}


//Imports.
import java.util.HashMap;
//********************************************************************
import java.util.Map;

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 obj)
    {

        map.put(obj.getEmployeeName(), obj);
    }
//********************************************************************
//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());
        }

    }


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


}

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





}
4

1 回答 1

8

因为这是作业,我只会指导你。


你有两个选择:

首先是创建额外的地图,映射id-Employeeemail- Employee,然后在添加新员工时填写所有三个地图。获取员工需要在其中一张地图上使用Map.get(key)方法。

其次,似乎更适合您的需要,选项是从地图中检索所有值 - 使用Map.values(),迭代它们(使用foreach),并检查给定员工id是否email是您正在寻找的人 - 使用object.equals(object2)方法。



最后一件事 - 尝试编写干净的代码,因此命名要准确 - 代替:

public void add(Employee obj)
{
    map.put(obj.getEmployeeName(), obj);
}

尝试以下:

public void add(Employee employee)
{
    map.put(employee.getEmployeeName(), employee);
}

确实有所作为,相信我:)

编辑:

回到命名建议-当您将类命名Employee为带有单词的多余名称方法时Employee-就像您对employee.getEmployeeName().

很明显,您想获得员工的姓名,而不是他的狗,也不是沙发: )(获得名为nameemployee.getName()的字段的值- 而不是myNameemployeeName)是您应该拥有的最简单和最好的主意:)

于 2012-06-27T16:52:58.223 回答