gpt4 book ai didi

java - 有效地对逻辑上属于同一实体的对象列表进行排序

转载 作者:行者123 更新时间:2023-11-29 03:06:36 24 4
gpt4 key购买 nike

我有一系列要以特定方式排序的对象。
示例:
我有课 Employee具有属性 departmentId .
这么多Employee的实例对象具有相同的值 departmentId .
员工也有薪水属性。
所以我有一个 List<Employee> employees我要排序的对象,排序顺序如下:
工资最低的员工在列表中排在第一位,然后是所有其他员工,这些员工按同一部门的工资排序。
然后在该部门的最后一名员工之后,我希望下一个部门的员工薪水低于所有其他部门,并对其余员工进行排序等。
例如。

(John, 10000, A)   
(Jane, 30000, A)
(Bill, 32000, A)
(Jim, 12000, B)
(Jake, 50000, B)
(James, 14000, C)

等等

完成此任务的最有效方法是什么?我想让代码尽可能紧凑和高效,而不是创建临时匿名类以将它们添加到 HashMap (除非这是唯一有效的方法)。

注意:
我知道 comparators 和 comparable 等等。
我的问题不是关于如何实际进行排序(我知道实现比较器)而是如何有效地完成这样的代码,最好避免一堆临时匿名对象

另外:我没有使用 Java 8,想要一个普通的 Java 方法

更新:
回应评论。我希望薪水最低的部门优先,然后是次高的部门,等等

最佳答案

我个人会使用 Employee类和 Department类,两者都将扩展 Comparable<T> 使用库功能的接口(interface)。

这是一个 Department 类的例子,它有一个名字和一个雇员列表,它们在构建时被排序。您可能想要浅拷贝 getEmployees() 上的列表防止其他人改变它的顺序:

class Department implements Comparable<Department> {

private List<Employee> employees;
private char name;

public Department(char name, List<Employee> employees) {
// avoid mutating original list
this.employees = new ArrayList<Employee>(employees);
this.name = name;

Collections.sort(this.employees);
}

@Override
public int compareTo(Department other) {
if (other == null) {
return 1;
}
// employees are sorted by salary within their department.
// all we need is to compare the lowest-salary employees
// from both departments
return this.employees.get(0).getSalary()
- other.employees.get(0).getSalary();
}

public List<Employee> getEmployees() {
return this.employees;
}

public char getName() {
return this.name;
}
}

现在Employee类只需要实现 compareTo(Employee other) 与薪资对比:

class Employee implements Comparable<Employee> {

private String name;
private int salary;

public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}

@Override
public int compareTo(Employee other) {
if (other == null) {
return 1;
}
return this.salary - other.salary;
}

@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + "]";
}

public String getName() {
return name;
}

public int getSalary() {
return salary;
}

}

这应该允许您使用 Collections.sort 在部门列表上并获得正确的顺序。这是一个完整的示例:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Fiddles {

static class Department implements Comparable<Department> {

private List<Employee> employees;
private char name;

public Department(char name, List<Employee> employees) {
// avoid mutating original list
this.employees = new ArrayList<Employee>(employees);
this.name = name;

Collections.sort(this.employees);
}

@Override
public int compareTo(Department other) {
if (other == null) {
return 1;
}
return this.employees.get(0).getSalary()
- other.employees.get(0).getSalary();
}

public List<Employee> getEmployees() {
return this.employees;
}

public char getName() {
return this.name;
}
}

static class Employee implements Comparable<Employee> {

private String name;
private int salary;

public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}

@Override
public int compareTo(Employee other) {
if (other == null) {
return 1;
}
return this.salary - other.salary;
}

@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + "]";
}

public String getName() {
return name;
}

public int getSalary() {
return salary;
}

}

public static void main(String args[]) {
final Department A = new Department('A', new ArrayList<Employee>() {
{
add(new Employee("John", 10000));
add(new Employee("Jane", 30000));
add(new Employee("Bill", 32000));
}
});
final Department B = new Department('B', new ArrayList<Employee>() {
{
add(new Employee("Jim", 12000));
add(new Employee("Jake", 50000));
}
});

final Department C = new Department('C', new ArrayList<Employee>() {
{

add(new Employee("James", 14000));

}
});

List<Department> departments = new ArrayList<Department>() {
{
add(A);
add(B);
add(C);
}
};
Collections.shuffle(departments);
Collections.sort(departments);
for (Department department : departments) {
for (Employee e : department.getEmployees()) {
System.out.println(String.format(
"Employee: %s, Salary: %d, department: %s",
e.getName(), e.getSalary(), department.getName()));
}
}
}
}

关于java - 有效地对逻辑上属于同一实体的对象列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31907198/

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