英文:
I have a PriorityQueue of Objects and I need to sort it based on float values.How do I create a comparator for it?
问题
我已经为优先队列创建了一个比较器。
class CompareBySalary implements Comparator<Employee> {
@Override
public int compare(Employee e1, Employee e2) {
return e1.salary < e2.salary ? -1 : e1.salary > e2.salary ? 1 : 0;
}
}
class Employee {
String name;
float salary;
Employee(String name, float salary) {
this.name = name;
this.salary = salary;
}
}
public class TryCode {
public static void main(String args[]) {
Employee e1 = new Employee("C", 10000);
Employee e2 = new Employee("A", 5000.45f);
Employee e3 = new Employee("D", 15000);
Employee e4 = new Employee("B", 5000.67f);
Queue<Employee> q = new PriorityQueue(new CompareBySalary());
q.offer(e1);
q.offer(e2);
q.offer(e3);
q.offer(e4);
for (Employee e : q)
System.out.println(e.name);
}
}
这会输出:A B D C
为什么它没有正确排序?
有什么我漏掉的东西吗?
附注:我已经尝试使用Float.compare(),它也会给出相同的输出。
英文:
I have already created a comparator for the priority queue.
class CompareBySalary implements Comparator<Employee>{
@Override
public int compare(Employee e1,Employee e2){
return e1.salary<e2.salary ? -1 : e1.salary>e2.salary ? 1 : 0;
}
}
class Employee{
String name;
float salary;
Employee(String name,float salary){
this.name=name;
this.salary=salary;
}
}
public class TryCode{
public static void main(String args[])
{
Employee e1=new Employee("C",10000);
Employee e2=new Employee("A",5000.45f);
Employee e3=new Employee("D",15000);
Employee e4=new Employee("B",5000.67f);
Queue<Employee> q=new PriorityQueue(new CompareBySalary());
q.offer(e1);
q.offer(e2);
q.offer(e3);
q.offer(e4);
for(Employee e:q)
System.out.println(e.name);
}
}
This gives the output:A B D C
Why is it not sorting correctly?
Is it something I am missing?
P.S. I have already tried with Float.compare() it gives the same output.
答案1
得分: 0
- 更改你的领域模型如下:
class Employee implements Comparable<Employee> {
private String name;
private float salary;
Employee(String name, float salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public float getSalary() {
return salary;
}
@Override
public int compareTo(Employee o) {
if (this.getSalary() > o.getSalary()) {
return 1;
} else if (this.getSalary() < o.getSalary()) {
return -1;
} else {
return 0;
}
}
}
-
移除
CompareBySalary
(你将不再需要它) -
创建你的
PriorityQueue
如下:
PriorityQueue<Employee> q = new PriorityQueue<>(Comparator.comparingDouble(Employee::getSalary));
英文:
-
Change your domain model as follows:
class Employee implements Comparable<Employee> { private String name; private float salary; Employee(String name, float salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public float getSalary() { return salary; } @Override public int compareTo(Employee o) { if (o1.getSalary()>o2.getSalary()) { return 1; } else if (o1.getSalary()<o2.getSalary()) { return -1; } else { return 0; } } }
-
Remove
CompareBySalary
(you won't need it) -
Create your
PriorityQueue
like this:PriorityQueue<Employee> q = new PriorityQueue<>(Comparator.comparingDouble(Employee::getSalary));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论