gpt4 book ai didi

java - 按多个变量对 List 进行排序的 compareTo 方法逻辑

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

问题:创建一个独立的 jar 可执行文件,它将按姓名、年龄和经验升序打印出现在面试中的候选人列表。

我在弄清楚 compareTo 方法逻辑以便能够对给定问题中的 3 个字段进行排序时遇到问题。

员工类

package com.example.demo.employee;

public class Employee implements Comparable<Employee> {

private String name;
private int age;
private int exp;

public Employee(String name, int age, int exp) {
super();
this.name = name;
this.age = age;
this.exp = exp;
}

public Employee() {
}

// getter setter

@Override
public int compareTo(Employee emp) {

// I do not think this logic is correct
// I have read the other stack overflow posts with similar problem
// but failing to under stand what to do in this method.

int result = (this.name).compareTo(emp.name);
if ( result == 0 ) {
result = (this.age).compareTo(emp.age);
}

if ( result == 0 ) {
result = (this.exp).compareTo(emp.exp);
}
return result;
}

}

员工服务类

package com.example.demo.employee;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {


public List<Employee> getEmployees() {

Employee e1 = new Employee("Sandhya", 20, 0);
Employee e2 = new Employee("Kemp", 24, 2);
Employee e3 = new Employee("Anil", 22, 3);
Employee e4 = new Employee("Kumar", 30, 6);
Employee e5 = new Employee("Tim", 32, 7);

public List<Employee> getEmployees() {

List<Employee> eList = new ArrayList<>();
eList.add(e1);
eList.add(e2);
eList.add(e3);
eList.add(e4);
eList.add(e5);

Collections.sort(eList);

return eList;
}
}

员工 Controller

package com.example.demo.employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmployeeController {

@Autowired
EmployeeService es;

@RequestMapping(value = "/")
public List<Employee> getEmpList(){
List<Employee> list = es.getEmployees();
return list;
}

}

最佳答案

无需实现Comparable和覆盖compareTo方法,只需使用Comparator

Comparator<Employee> c = Comparator.comparing(Employee::getName)
.thenComparing(Employee::getAge)
.thenComparing(Employee::getExp);

并使用 Collections.sort() 使用传递的 Comparator 对列表进行排序

Collections.sort(eList,c);

通过使用 Comparable

问题是 ageexpint 类型,如果你不能使用 compareTo 方法,它是原始类型,将它们的类型更改为 Integer 包装器对象或使用 Integer.compare(int a, int b) 方法

private int age;    // to private Integer age
private int exp; // to private Integer exp

这样你就可以在 ageexp 上使用 compareTo

this.getAge().compareTo(o.getAge());
this.getExp().compareTo(o.getExp());

如果看不到我下面使用 Integer.compare(int a, int b)

的解决方案

解决方案

@Override
public int compareTo(Employee o) {
int result = this.getName().compareTo(o.getName());
if (result == 0) {
result = Integer.compare(this.getAge(), o.getAge());
if (result == 0) {
return Integer.compare(this.getExp(), o.getExp());
}
return result;
}
return result;
}

关于java - 按多个变量对 List 进行排序的 compareTo 方法逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56033151/

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