gpt4 book ai didi

java - 如何对 HashMap> 中的员工列表进行排序?

转载 作者:行者123 更新时间:2023-11-30 06:22:10 24 4
gpt4 key购买 nike

这是我的主类,我的代码 ID 在此实现,并添加员工和部门并进行排序..

    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;
}
}
}

/*删除列表中员工的代码*/

    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();
}
}

这是我的员工类,我的排序逻辑在其中实现::

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;
}

/* 这是我的排序代码,我正在根据 ID 和工资进行工作,但我不知道是否可以正常运行。

    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 ..

最佳答案

员工必须实现Comparable而不是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;
}

然后可以调用Collections.sort(list);对ArrayList进行排序。

关于java - 如何对 HashMap<String,List<Employee>> 中的员工列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com