Java 比较方法逻辑以按多个变量排序列表

Java 比较方法逻辑以按多个变量排序列表,java,list,sorting,comparable,Java,List,Sorting,Comparable,问题:创建一个独立的jar可执行文件,它将按姓名、年龄和经验升序打印面试候选人名单 我在计算compareTo方法逻辑以对给定问题中的3个字段进行排序时遇到困难 雇员阶级 package com.example.demo.employee; public class Employee implements Comparable<Employee> { private String name; private int age; private int exp; public Em

问题:创建一个独立的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;
公共类Employee实现了可比较的{
私有字符串名称;
私人互联网;
私人进出口;
公共雇员(字符串名称、整数年龄、整数经验){
超级();
this.name=名称;
这个。年龄=年龄;
this.exp=exp;
}
公职人员(){
}
//吸气剂设定器
@凌驾
公共内部比较(员工环境管理计划){
//我认为这个逻辑不正确
//我读过其他有类似问题的堆栈溢出帖子
//但是没有理解用这种方法做什么。
int result=(this.name).compareTo(emp.name);
如果(结果==0){
结果=(此年龄)与(emp年龄)相比;
}
如果(结果==0){
结果=(this.exp).compareTo(emp.exp);
}
返回结果;
}
}
员工服务类

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;
  }
}
package com.example.demo.employee;
导入org.springframework.stereotype.Service;
导入java.util.List;
@服务
公营雇员服务{
公开名单{
员工e1=新员工(“Sandhya”,20,0);
员工e2=新员工(“Kemp”,24,2);
员工e3=新员工(“Anil”,22,3);
雇员e4=新雇员(“Kumar”,30,6);
员工e5=新员工(“Tim”,32,7);
公开名单{
List eList=new ArrayList();
列表添加(e1);
添加(e2);
添加列表(e3);
添加(e4);
列表添加(e5);
集合。排序(eList);
返回列表;
}
}
雇员控制员

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

}
package com.example.demo.employee;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入java.util.List;
@RestController
公共类EmployeeController{
@自动连线
雇员服务;
@请求映射(value=“/”)
公共列表getEmpList(){
List List=es.getEmployees();
退货清单;
}
}

无需实现
Comparable
和重写
compareTo
方法,只需使用
Comparator

Comparator<Employee> c = Comparator.comparing(Employee::getName)
                                       .thenComparing(Employee::getAge)
                                       .thenComparing(Employee::getExp);
Collections.sort(eList,c);
使用可比的

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

private int age;    // to private Integer age
private int exp;    // to private Integer exp
这样您就可以在
age
exp

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中的类型
int
String
非常不同。
int
是一种所谓的基元类型(因为它不由其他类型组成)是一种对象类型。按照惯例,基本类型的名称以小写字母开头,而对象类型的名称以大写字母开头

只有对象类型才能有方法

由于
this.age
的类型是
int
,因此是一个基本类型,
(this.age).compareTo(…)
是不允许的。相反,您必须编写
Integer.compare(this.age,emp.age)


编译器的错误消息并没有真正的帮助。与其说“method int.compareTo not found”,不如说“this.age的类型是int,因为这是一个基本类型,所以不能对它调用任何方法”.

因此,问题似乎是compareTo中没有出现自动装箱,因此您可以将其装箱,或者将age/exp作为原始INT处理。 因此,将具有compareTo的原语
int
转换为
Integer

public int compareTo(Employee emp) {
    int result = (this.name).compareTo(emp.name);
    if ( result == 0 ) {
        result = Integer.valueOf(age).compareTo(emp.age);
    }
    if ( result == 0 ) {
        result = Integer.valueOf(exp).compareTo(emp.exp);
    }
    return result;
 }
或者,您也可以这样做,不使用原语:

public int compareTo(Employee emp) {
    int result = (this.name).compareTo(emp.name);
    if ( result == 0 ) {
        result = Integer.compare( age, emp.age );
    }
    if ( result == 0 ) {
        result = Integer.compare( exp, emp.exp );
    }
    return result;
 }
public类Employee实现了可比较的{
私有字符串名称;
私人互联网;
私人进出口;
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
public int getExp(){
返回经验;
}
公共无效setExp(int-exp){
this.exp=exp;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共雇员(字符串名称、整数年龄、整数经验){
超级();
this.name=名称;
这个。年龄=年龄;
this.exp=exp;
}
公职人员(){
}
@凌驾
公共内部比较(员工环境管理计划){
//在这里更换比较器
返回(this.getAge()-emp.getAge());
}
}
-------------------------------------------------------
@服务
公营雇员服务{
公开名单{
List emp=new ArrayList();
员工emp1=新员工(“Sandhya”,20,0);
员工emp2=新员工(“Kemp”,24,2);
员工emp3=新员工(“Anil”,22,3);
员工emp4=新员工(“Kumar”,30,6);
员工emp5=新员工(“Tim”,32,7);
emp.add(emp1);
emp.add(emp2);
emp.add(emp3);
emp.add(emp4);
emp.add(emp5);
返回emp;
}
}
---------------------------------------------------------------------------
@RestController
公共类EmployeeController{
@自动连线
员工服务员工服务;
@请求映射(“/”)
公共列表getEmpList(){
List empController=employeeService.getEmployees();
Collections.sort(empController);
返回控制器;
}
}
你为什么认为逻辑是正确的
public class Employee implements Comparable<Employee> {

    private String name;
    private int age;
    private int exp;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getExp() {
        return exp;
    }
    public void setExp(int exp) {
        this.exp = exp;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Employee(String name, int age, int exp) {
        super();
        this.name = name;
        this.age = age;
        this.exp = exp;
    }
    public Employee() {
  }

    @Override
    public int compareTo(Employee emp) {
    //replace your comparator here
       return (this.getAge() - emp.getAge());
  }

}
-------------------------------------------------------
@Service
public class EmployeeService {


    public List<Employee> getEmployees() {
        List<Employee> emp = new ArrayList<>();
        Employee emp1 = new Employee("Sandhya",20,0);
        Employee emp2 = new Employee("Kemp",24,2);
        Employee emp3 = new Employee("Anil",22,3);
        Employee emp4 = new Employee("Kumar",30,6);
        Employee emp5 = new Employee("Tim",32,7);
        emp.add(emp1);
        emp.add(emp2);
        emp.add(emp3);
        emp.add(emp4);
        emp.add(emp5);

        return emp;
    }




}
---------------------------------------------------------------------------

@RestController
public class EmployeeController {

  @Autowired
  EmployeeService employeeService;

  @RequestMapping("/")
    public List<Employee> getEmpList(){
    List<Employee> empController = employeeService.getEmployees();
    Collections.sort(empController);
        return empController;
    }

}