删除方法不起作用(使用Hashmap)

时间:2012-07-23 15:30:01

标签: java hashmap

我正在建立一个员工店。 employeeStore中的删除功能不起作用。

,而不是删除员工,它不会做任何事情

这是我的代码: 主

case 3:
                System.out.println("Delete by Name.");
                Employee employeeDelete = MenuMethods.userInputByName();
                Store.searchByName(employeeDelete.getEmployeeName());
                System.out.println("Your choice is: "+ employeeDelete);
                Store.remove(employeeDelete);
                break;

删除方法

// ---------------------------------------------------------------------------------------
// Name: Remove.
// ---------------------------------------------------------------------------------------
public Employee remove(Employee key) {
    // Remove the Employee by name.
    if (map.containsKey(key))
        return map.remove(key); // if its there remove and return
    else
        return null; // if its not there return 0x00000000 address
}

菜单方法

//---------------------------------------------------------------------------------------
//  Name:           Imports.
//  Description:    To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.util.Scanner;
//---------------------------------------------------------------------------------------
public class MenuMethods 
{
    private static Scanner keyboard = new Scanner(System.in);
//---------------------------------------------------------------------------------------
//  Methods for the Company Application menu.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
//  Name:           getMenuChoice.
//  Description:    Method for validating the choice.
//---------------------------------------------------------------------------------------
    public static int getMenuChoice(String menuString, int limit,String prompt, String errorMessage) 
    {
        System.out.println(menuString);
        int choice = inputAndValidateInt(1, limit, prompt, errorMessage);
        return choice;
    }

//---------------------------------------------------------------------------------------
//  Name:        inputAndValidateInt.
//  Description: This method is used in the getMenuChoice method.
//---------------------------------------------------------------------------------------
    public static int inputAndValidateInt(int min, int max, String prompt,String errorMessage) 
    {
        int number;
        boolean valid;
        do 
        {
            System.out.print(prompt);
            number = keyboard.nextInt();
            valid = number <= max && number >= min;
            if (!valid) 
            {
                System.out.println(errorMessage);
            }
        } while (!valid);
        return number;
    }

//---------------------------------------------------------------------------------------
//  Name:        userInput
//  Description: This method is used in the MainApp to give the user capability to enter
//               the details when adding details of an employee into the store.
//---------------------------------------------------------------------------------------
    public static Employee userInput() 
    {
        String temp = keyboard.nextLine();
        Employee e = null;
        System.out.println("Please enter the Employee Name:");
        String employeeName = keyboard.nextLine();
        System.out.println("Please enter the Employee ID:");
        int employeeId = keyboard.nextInt();
        temp = keyboard.nextLine();
        System.out.println("Please enter the Employee E-mail address:");
        String employeeEmail = keyboard.nextLine();
        return e = new Employee(employeeName, employeeId, employeeEmail);

    }

//---------------------------------------------------------------------------------------
//  Name:        userInputByName.
//  Description: This method is used in the MainApp to give the user capability to search by name.
//---------------------------------------------------------------------------------------
    public static Employee userInputByName() 
    {
        // String temp is for some reason needed. If it is not included
        // The code will not execute properly.
        String temp = keyboard.nextLine();
        Employee e = null;
        System.out.println("Please enter the Employee Name:");
        String employeeName = keyboard.nextLine();

        return e = new Employee(employeeName);

    }

//---------------------------------------------------------------------------------------
//  Name:        userInputByEmail
//  Description: This method is used in the MainApp to give the user capability to search by email.
//---------------------------------------------------------------------------------------
    public static String userInputByEmail() 
    {
        // String temp is for some reason needed. If it is not included
        // The code will not execute properly.
        String temp = keyboard.nextLine();
        Employee e = null;
        System.out.println("Please enter the Employee Email:");
        String employeeEmail = keyboard.nextLine();
        // This can use the employeeName's constructor because java accepts the
        // parameters instead
        // of the name's.
        return employeeEmail;

    }
//---------------------------------------------------------------------------------------

}

员工

//---------------------------------------------------------------------------------------
//  Employee class.
//---------------------------------------------------------------------------------------
public class Employee
{
//---------------------------------------------------------------------------------------
//  Variables to be used in the employee store.
//---------------------------------------------------------------------------------------
    private String employeeName;
    private int employeeId;
    private String employeeEmail;
//---------------------------------------------------------------------------------------
//  Name:        Constructors.
//  Description:
//---------------------------------------------------------------------------------------
    public Employee(String employeeName, int employeeId, String employeeEmail) 
    {
        this.employeeName = employeeName;
        this.employeeId = employeeId;
        this.employeeEmail = employeeEmail;
    }
//---------------------------------------------------------------------------------------
//  Overloading the constructor for the use with userInputByName method.
//---------------------------------------------------------------------------------------
    public Employee(String employeeName) 
    {
        this.employeeName = employeeName;
    }
//---------------------------------------------------------------------------------------
//  Name:   Getters.
//---------------------------------------------------------------------------------------
    public String getEmployeeEmail() 
    {
        return employeeEmail;
    }

    public String getEmployeeName() 
    {
        return employeeName;
    }
    public int getEmployeeId() 
    {
        return employeeId;
    }
//---------------------------------------------------------------------------------------
//  Name:   Setters.
//---------------------------------------------------------------------------------------
    public void setEmployeeEmail(String employeeEmail) 
    {
        this.employeeEmail = employeeEmail;
    }
    public void setEmployeeName(String employeeName) 
    {
        this.employeeName = employeeName;
    }
    public void setEmployeeId(int employeeId)
    {
        this.employeeId = employeeId;
    }

//---------------------------------------------------------------------------------------
//  Name:   toString.
//---------------------------------------------------------------------------------------
    public String toString() 
    {
        return "\t\t\tEmployee\n" +
                "********************************************************************\n"+
                "Employee Name: "+ employeeName +"\n"+ 
                "Employee Id: " + employeeId +"\n"+  
                "Employee Email: " + employeeEmail;
    }
//---------------------------------------------------------------------------------------
}

3 个答案:

答案 0 :(得分:3)

此方法

public static Employee userInputByName

正在创建 Employee。 (它还将它无意义地分配给局部变量......代码样式在这里非常奇怪 - 就像对nextLine()的无关调用一样。)

除非您在equals中覆盖了hashCodeEmployee,否则它不会认为 new Employee对象等于HashMap中现有的一个。遗憾的是,您还没有向我们展示Employee类,但是如果它们具有相同的名称,则可能是为了将员工进行比较(并给出相同的哈希码)。

答案 1 :(得分:1)

您需要做的就是

public Employee remove(Employee key) {
    // Remove the Employee by name.
    return map.remove(key); // if its there remove and return
}

此问题的最常见原因是hashCode和equals不正确,不匹配或依赖于可变字段。

答案 2 :(得分:1)

经典:你可能在Employee对象上缺少equals()和hashcode()。您还应该考虑为地图使用一个简单的密钥,例如employee.id:就像现在一样,您的EmployeeMap可以更好地建模为EmployeeSet。

在代码中进一步查看,您可能希望创建多个Maps作为员工对象的索引:

Map<Integer, Employee> employeeById
Map<String, Employee> employeeByEmail;
MultiMap<String, Employee> employeeByName; 

请注意最后一行中建议的com.google.common.collect.Multimap,以便可能包含多个共享相同John Doe名称的员工参考。