问题:创建一个独立的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;
}
}
EmployeeController
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;
}
}
答案 0 :(得分:2)
无需实现Comparable
和重写compareTo
方法,只需使用Comparator
Comparator<Employee> c = Comparator.comparing(Employee::getName)
.thenComparing(Employee::getAge)
.thenComparing(Employee::getExp);
然后使用Collections.sort()
通过传递的Comparator
对列表进行排序
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
这样您就可以在compareTo
和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;
}
答案 1 :(得分:1)
您的代码看起来非常好,我不得不思考一下为什么它可能根本是错误的。
问题在于Java中的int
和String
类型是非常不同的。 int
是所谓的原始类型(因为它不是由其他类型组成的),而String
是对象类型。按照约定,基本类型的名称以小写字母开头,而对象类型的名称以大写字母开头。
仅对象类型可以具有方法。
由于this.age
的类型为int
,因此是原始类型,因此不允许(this.age).compareTo(...)
。相反,您必须编写Integer.compare(this.age, emp.age)
。
编译器的错误消息并没有真正的帮助。与其说“找不到方法int.compareTo”,不如说“ this.age的类型是int,并且由于它是原始类型,所以不能在其上调用任何方法”。
答案 2 :(得分:1)
因此,问题似乎在于compareTo中没有发生自动装箱,因此您可以将它们装箱,或者仅将age / exp作为原始整数处理。
这样做,将原始int
转换为具有compareTo的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;
}
答案 3 :(得分:0)
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;
}
}
答案 4 :(得分:0)
对排序问题代码的正确修改是
//In service class
public List<Employee> getEmployees() {
Employee e1 = new Employee("Sandhya", 24, 0);
Employee e2 = new Employee("Kemp", 22, 2);
Employee e3 = new Employee("Anil", 26, 3);
Employee e4 = new Employee("Kumar", 30, 6);
Employee e5 = new Employee("Tim", 32, 7);
List<Employee> elist = Arrays.asList(e1,e2,e3,e4,e5);
return elist;
//in controller class
@Autowired
EmployeeService empserv;
@RequestMapping(value="/")
public List<Employee> getEmpList(){
List<Employee> list = empserv.getEmployees();
Collections.sort(list);
return list;
}
//in Employee class, since it was asked to sort according to age of the employees
@Override
public int compareTo(Employee o) {
int result = Integer.compare(this.getAge(), o.getAge());;
/*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;
}
答案 5 :(得分:0)
为了通过这个测试。
在Employee类中添加
public int compareTo(Employee emp) { 返回 this.getAge() - emp.getAge();
}
在 EmployeeService 中,只需在 getEmployess 方法中复制 TestEmployee 类中的列表数据并返回列表即可。
在EmployeeController中添加@RequestMapping("/")并使用Collections.sort(list)和返回列表;