Tuesday, April 29, 2014

Employee Department One to Many Relationship implemented using hashMap In java

As we all know that When we are preparing for Java Interviews for some big giants including all investment banks, or topmost companies including Amazon,Oracle or Alcatel you will definitely encounter questions from core java and data structures including algorithmic approach of any given problem. To cope up with, i have come up with this blog. When we go for a java interview, recently to GoldMann Sachhs i was able to reach till 6th round and then they put me on halt.





So i would like to share my experience with u on the interview questions.
I want to start this journey with a tough question directly. He asked me that you are given a Table Employee and a Department in the Database, now the extracted data is present with you from the Database. Now you have to design such a dataStructure which will print all the data combined. Firstly i was dumbstruck, as he is asking the combo or join of two tables in java. Then i gave him this solution.

1) I will create an Employee Class which has employeeId and employeeName as atributes which will serve as a model class including getters and setter s defined.



public class Employee {

int employeeId;
String employeeName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

@Override

public String toString() {
     return ("Employee Id:"+this.getEmployeeId()+" "+"Employee Name:"+this.getEmployeeName());
}
}

2) I will create a Department class which has departmentName as its atribute.



public class Department {

String departmentName;


public String getDepartmentName() {

return departmentName;
}

public void setDepartmentName(String departmentName) {

this.departmentName = departmentName;
}
}


Now i will create the main class as HashMapQuestion.java class which will contain 



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HashMapQuestin {

public static void main(String[] args) {
Map<Department, List<Employee>> hashMap = new HashMap<Department, List<Employee>>();
Department d1 = new Department();
d1.setDepartmentName("Computer Science");
Department d2 = new Department();
d2.setDepartmentName("Information Technology");
Employee e1 = new Employee();
e1.setEmployeeId(1);
e1.setEmployeeName("Ankur");
Employee e2 = new Employee();
e2.setEmployeeId(2);
e2.setEmployeeName("Annu");
List<Employee> employeeListOne = new ArrayList<Employee>();
employeeListOne.add(e1);
employeeListOne.add(e2);
Employee e3 = new Employee();
e3.setEmployeeId(3);
e3.setEmployeeName("Vishank");
Employee e4 = new Employee();
e4.setEmployeeId(4);
e4.setEmployeeName("Sonu");
List<Employee> employeeListTwo = new ArrayList<Employee>();
employeeListTwo.add(e3);
employeeListTwo.add(e4);
hashMap.put(d1, employeeListOne);
hashMap.put(d2, employeeListTwo);
for (Map.Entry<Department, List<Employee>> entry : hashMap.entrySet()) {
Department key = entry.getKey();

System.out.println("The Departments are: "
+ key.getDepartmentName());
List<Employee> employees = entry.getValue();
System.out
.println("The Employees belonging to tis department are: ");
for (Employee employee : employees) {
System.out.println(employee);
}

}
}

}


This will solve our purpose. As it contains a HashMap which has Department as the Key and List of Employees as Value in HashMap(K,V) pair.  Many employees can have same department or one department is attached to many employees. i.e Department has one to many relationship with Employees.

So we are maintaining  a list of employees as values and a particular department as a key.

If you find any queries, please feel free to ask or comment.




No comments:

Post a Comment