我需要一个hashmap的搜索方法,但我无法弄清楚如何去做。我也试图做一个编辑方法,我认为我需要这个方法。我的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 + "]";
}
//********************************************************************
}
答案 0 :(得分:7)
由于这是家庭作业,我只会指导你。
您有两种选择:
首先是创建其他地图,映射id
- Employee
和email
- Employee
,然后填写所有三张地图添加新员工。获得员工需要在其中一张地图上使用Map.get(key)方法。
第二次,似乎更适合您的需求,选项是从地图中检索所有值 - 使用Map.values(),迭代它们(使用{{3 }}),并使用id
方法检查给定员工的email
或object.equals(object2)
是否是您正在寻找的员工。
最后一件事 - 试着写foreach,所以要精确命名 - 代替:
public void add(Employee obj)
{
map.put(obj.getEmployeeName(), obj);
}
尝试以下:
public void add(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
确实有所作为,相信我:)
修改强>
返回命名建议 - 如果您将Employee
命名为Employee
,那么其名称为employee.getEmployeeName()
的名称方法,就像您使用employee.getName()
一样。
很明显,你想获得empoyee的名字,而不是他的狗,也不想沙发:) {{1}}(获得名为 name 的字段值 - 不是 myName 或 employeeName )是最简单和最好的想法,你应该:)