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. I have the MainApp
, Employee class and an EmployeeStore
class. 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. Getting employee would require to use Map.get(key) method on one of those maps.
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.
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()
.
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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.