1

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 ..
  • 1
    I did not read all that bunch of code (explaining the problem instead of showing the code would be more useful IMO), but I think this may help https://stackoverflow.com/questions/7427758/how-to-use-sortedmap-interface-in-java – ilmirons Dec 20 '17 at 09:12
  • Possible duplicate of [How to use SortedMap Interface in Java?](https://stackoverflow.com/questions/7427758/how-to-use-sortedmap-interface-in-java) – Sal-laS Dec 20 '17 at 09:19

2 Answers2

1

Employee must implement Comparable instead of Comparator.

public int compareTo(Employee o) {
    int res = empId.compareTo(o.empId);

    if (res != 0)
        return res;

    res = empName.compareTo(o.empName);

    if (res != 0)
        return res;

    res = empSalary.compareTo(o.empSalary);

    if (res != 0)
        return res;

    return 0;
}

Then you can call Collections.sort(list); to sort ArrayList.

SomeFire
  • 170
  • 4
0

What you need is interface Comparable instead of Comparator. Comparable defines so called "natural ordering" of objects. Comparator should be implemented by a separate class when you need to compare objects with different ordering than the natural ordering (default).

pkalinow
  • 1,619
  • 1
  • 17
  • 43