0

我目前正在写一个员工商店,我需要能够删除商店的内容。

我该怎么做?

我已经有一个使用密钥删除它的删除方法。但是当我在互联网上找到这段代码时,我不知道如何使用它。

这是我的代码:

主应用

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();


    }

}

员工商店

//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

2 回答 2

1

EmployeeStore使用 aHashMap来存储员工。每个员工在地图中都由其名称标识。要删除员工,您只需调用EmployeeStore.remove(String employeeName)

EmployeeStore employeeStore = new EmployeeStore();
employeeStore.add(new Employee ("James O' Carroll", 18,"hotmail.com"));
employeeStore.add(new Employee ("Andy Carroll", 1171,"yahoo.com"));
employeeStore.add(new Employee ("Luis Suarez", 7,"gmail.com"));

// Remove a certain employee
employeeStore.remove("Andy Carroll");

// Remove all employees
employeeStore.clear();

您应该注意两点:

1 - 当前的实现不允许两个或多个员工有相同的名字,因为这些名字被用作映射中的键。

2 - 当您调用该remove方法时,您必须提供确切的名称,因为StringJava 中的相等是区分大小写的。

您可能还想看看Map.

于 2012-06-23T20:25:42.253 回答
0

A. 为什么不使用 HashMap 的 remove 方法来解决这个问题?
B. 此外,也许您希望通过某些标准执行删除 - 为此您必须在地图上进行迭代,找到相关条目,获取其键,并按照 A 部分所述调用删除

于 2012-06-23T20:26:35.207 回答