我目前正在编写一个Employee商店,我需要能够删除商店的内容。
我该怎么做?
我已经有一个删除方法使用键删除它。但是当我在互联网上发现这个代码时,我不知道如何使用它。
这是我的代码:
MainApp
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();
}
}
EmployeeStore
//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 + "]";
}
//********************************************************************
}
答案 0 :(得分:1)
您的EmployeeStore
使用HashMap
来存储员工。每个员工都在地图中以其名称标识。要删除员工,只需拨打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
方法时,您必须提供确切的名称,因为Java中的String
相等区分大小写。
您可能还想查看Map
。
答案 1 :(得分:0)
一个。为什么不使用HashMap的remove方法来解决这个问题呢?
B.此外,您可能希望按照某些标准执行删除 - 为此,您必须在地图上进行迭代,找到相关条目,获取其密钥,并按照A部分所述调用删除