简体   繁体   English

哈希图的搜索方法

[英]Search Method for Hashmap

I need for Search method for a hashmap and I cant quite figure out how to do it. 我需要一个哈希图的搜索方法,我不能完全弄清楚该怎么做。 I'm also trying to do a edit method and I think for this I need the method. 我也在尝试做一个编辑方法,为此我需要这种方法。 My hashMap is to store employee data. 我的hashMap是要存储员工数据。 I have the MainApp , Employee class and an EmployeeStore class. 我有MainApp ,Employee类和EmployeeStore类。 Can anyone help? 有人可以帮忙吗?

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





}

Since this is homework I'll only guide you. 因为这是家庭作业,所以我只会指导您。


You have two options: 您有两种选择:

First of them is to create additional maps, mapping id - Employee and email - Employee , then fill all three maps when adding new employee. 首先是创建其他地图,映射id - Employeeemail - Employee ,然后在添加新员工时填写所有三张地图。 Getting employee would require to use Map.get(key) method on one of those maps. 招募员工将需要在其中一张地图上使用Map.get(key)方法。

Second , seems like better suiting your need, option is to retrieve all values from map - using Map.values() , iterate over them (using foreach ), and check if id or email of given employee is the one that you was looking for - using object.equals(object2) method. 其次 ,似乎更适合您的需要, 选项是从map检索所有值-使用Map.values() ,对其进行迭代(使用foreach ),并检查给定员工的idemail是否是您要找的那个-使用object.equals(object2)方法。



and one last thing - try to write clean code , so be precise in naming - in place of: 最后一件事-尝试编写简洁的代码 ,因此要精确命名-代替:

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

try following: 尝试以下:

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

It does make the difference, trust me :) 确实有所作为,相信我:)

EDIT: 编辑:

Going back to naming advice - When you have class Named Employee its redundant to name method with word Employee within - as you did with employee.getEmployeeName() . 回到命名建议 -当您拥有Named Employee类时,其内含单词Employee多余的name方法-就像对employee.getEmployeeName()

It's quite obvious, that you want to get name of empoyee, not his dog, nor couch :) employee.getName() (that gets value of field named name - not myName or employeeName ) is simpliest and best idea that you shoud have :) 很明显,您想要获取empoyee的名称,而不是他的狗,也不要沙发:) employee.getName() (获取名为name的字段的值-不是myNameemployeeName )是最简单的,最好的主意是: )

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

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