this is my main class where my code id implemented and adding employees and department and sorting ..
protected void addEmployee() {
HashMap<String, List<Employee>> mapinfo;
Scanner read;
mapinfo = new HashMap<String, List<Employee>>();
ArrayList<Employee> employeeList1 = new ArrayList<Employee>();
employeeList1.add(new Employee(1001, "AA Ishant Kumar", 4500));
employeeList1.add(new Employee(1002, "AA Nitin Tripathi", 1300));
employeeList1.add(new Employee(1004, "AA Nitin Tripathi", 1300));
employeeList1.add(new Employee(1006, "AA Nitin Tripathi", 1300));
employeeList1.add(new Employee(1002, "AA Kailash Pathak", 1200));
employeeList1.add(new Employee(1002, "AA Kailash Pathak", 1500));
employeeList1.add(new Employee(1002, "AA Kailash Pathak", 1100));
ArrayList<Employee> employeeList2 = new ArrayList<Employee>();
employeeList2.add(new Employee(2004, "BB Ishant Kumar", 4521));
employeeList2.add(new Employee(2006, "BB Kailash Pathak", 6532));
employeeList2.add(new Employee(2005, "BB-Nitin-Tripathi", 9884));
employeeList2.add(new Employee(2007, "BB-Nitin-Tripathi", 9975));
employeeList2.add(new Employee(2004, "BB-Nitin-Tripathi", 9576));
ArrayList<Employee> employeeList3 = new ArrayList<Employee>();
employeeList3.add(new Employee(3007, "CC Nitin Tripathi", 2015));
employeeList3.add(new Employee(3009, "CC Kailash Pathak", 6987));
employeeList3.add(new Employee(3010, "CC-Playuce-Change", 1567));
employeeList3.add(new Employee(3010, "CC-Ishant-Kumar", 1897));
mapinfo.put("SUPPORT", employeeList2);
mapinfo.put("ACCOUNTING", employeeList1);
mapinfo.put("JAVA-TEAM", employeeList3);
}
protected void insertNewEmployee() {
List<Employee> employee = new ArrayList<Employee>();
read = new Scanner(System.in);
System.out.println("Which Department you want to add Employee");
String comparedept = read.next().toUpperCase();
System.out.println("Enter Employee ID");
int empid = read.nextInt();
System.out.println("Enter Employee Name");
String empname = read.next();
System.out.println("Enter Employee Salary");
int empsalary = read.nextInt();
employee.add(new Employee(empid, empname, empsalary));
for (int i = 0; i < mapinfo.size(); i++) {
if (mapinfo.containsKey(comparedept)) {
mapinfo.get(comparedept).addAll(employee);
break;
} else {
mapinfo.put(comparedept, employee);
break;
}
}
}
/* code for delete employee in list*/
protected void deleteEmployee() {
read = new Scanner(System.in);
System.out.println("In which Department you want to delete Employee");
String depname = read.next().toUpperCase();
System.out.println("Tell me Employee ID ");
int empid = read.nextInt();
Set<?> set = mapinfo.entrySet();
Iterator<?> itr = set.iterator();
while(itr.hasNext())
{
Map.Entry<String,List<Employee>> mapentry = (Map.Entry<String,List<Employee>>) itr.next();
//String deptName = mapentry.getKey();
List<Employee> emplist = mapentry.getValue();
for(int k=0;k<emplist.size();k++)
{
if(emplist.get(k).getEmpId()==empid)
{
emplist.remove(k);
mapinfo.put(depname, emplist);
break;
}
}
}
}
protected void displayAll() {
// Set<?> set = mapinfo.entrySet();
// Iterator<?> itr = set.iterator();
// while(itr.hasNext())
// {
// Map.Entry<String,Employee> mapentry = (Map.Entry)itr.next();
// String key = mapentry.getKey();
// Employee emp = mapentry.getValue();
TreeMap<String, List<Employee>> sorted = new TreeMap<>(mapinfo);
Set<Entry<String, List<Employee>>> sortdept = sorted.entrySet();
System.out.println("HashMap after sorting by Dept Name(keys) in ascending order ");
for (Entry<String, List<Employee>> listemp : sortdept) {
String key = listemp.getKey();
List<Employee> listofemp = listemp.getValue();
this is my sorting code call from collections class and implementation is in Employee class..
**Collections.sort(listofemp,new Employee());**
System.out.println("----------------------------------------------------------------------");
System.out.println("Department : " + key);
if (listofemp != null)
for (Employee employee : listofemp) {
System.out.println("Employee ID : " + employee.getEmpId() + "\tEmployee Name : "
+ employee.getEmpName() + "\tEmployee Salary : " + employee.getEmpSalary());
}
}
}
protected void chooseOptionToInsertNewItem() {
read = new Scanner(System.in);
System.out.println("For add employee or department ENTER 1 and delete employee ENTER 2 or for EXIT ENTER 0");
int input = read.nextInt();
while (input != 0) {
if (input == 1) {
insertNewEmployee();
displayAll();
} else if (input == 2) {
deleteEmployee();
displayAll();
}
System.exit(0);
}
}
public static void main(String[] args) {
EmployeeInfo empinfo = new EmployeeInfo();
empinfo.addEmployee();
empinfo.displayAll();
empinfo.chooseOptionToInsertNewItem();
}
}
And this is my Emmployee Class where my sort logic is implemented ::
public class Employee implements Comparator<Employee> {
private Integer empId;
private String empName;
private Integer empSalary;
public Employee(Integer empId, String empName, Integer empSalary) {
this.empId = empId;
this.empName = empName;
this.empSalary = empSalary;
}
protected Integer getEmpId() {
return empId;
}
protected Integer getEmpSalary() {
return empSalary;
}
protected String getEmpName() {
return empName;
}
/* this is my sortin code where i'm working on id and salary basis but i don't know not running properly.
public int compare(Employee e,Employee ee) {
if (e.empId > ee.empId)
return 1;
else if (e.empId == ee.empId)
{
return e.empSalary.compareTo(e.empSalary);
}
else
return -1;
}
Employee() { }
}
Help to sort on the basis of salary and name using comparator ..