gpt4 book ai didi

java - 如何将比较器与多个字段对象一起使用?

转载 作者:行者123 更新时间:2023-11-30 02:44:40 26 4
gpt4 key购买 nike

我尝试使用界面比较器来排序优先队列,以便乘客的顺序首先取决于他们是否有残疾,然后取决于他们拥有的机票类型,最后取决于到达时间。

import java.util.*;

public static void main(String[] args){
Random rand = new Random(System.nanoTime());
Comparator<Passenger> comparator;
PriorityQueue<Passenger> queue = new PriorityQueue<Passenger>(10, comparator);
Passenger pass[] = new Passenger [10];


for (int i=0; i<10;i++){
int time1 = 0;
pass[i] = new Passenger(rand.nextInt(100000000), rand.nextInt(3) , rand.nextBoolean(), time1);
time1 = time1 + 15;
}

}

在那里我初始化了乘客数组,这是 Passenger 类和比较方法:

public class Passenger implements Comparator<Passenger>{

private int ID;
private int clase;
private boolean disability;
private int arrivalTime;

public Passenger(int ID, int clase, boolean disability, int arrivalTime) {

this.ID = ID;
this.clase = clase; // 0-vip 1-economy 2-economy
this.disability = disability;
this.arrivalTime = arrivalTime;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public int getClase() {
return clase;
}
public void setClase(int clase) {
this.clase = clase;
}
public boolean isDisability() {
return disability;
}
public void setDisability(boolean disability) {
this.disability = disability;
}
public int getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}

public int compare(Passenger pas1, Passenger pas2) {
if((pas1.isDisability()) && (!pas2.isDisability())){
return 1; //passenger 1 has disability
}else if((!pas1.isDisability()) && (pas2.isDisability())){
return -1; //passenger 2 has disability
}
else{ //both have disability or no one has disability
if(pas1.getClase() < pas2.getClase()){
return 1; // passenger 1 has better class
}else if(pas1.getClase() > pas2.getClase()){
return -1; // passenger 2 has better class
}
else{ //both have disability and same class
if(pas1.getArrivalTime() < pas2.getArrivalTime()){
return 1; //passenger 1 arrived before passenger 2
}
else return -1; //passenger 2 arrived before passenger 1
}
}
}

如何更好地处理这些多级比较?

最佳答案

您的问题似乎是关于简化比较,但我认为您宁愿实现 Comparable<Passenger>而不是Comparator ,并使用#compareTo方法。至于清理,如果您只是抽象实际的 boolean 逻辑,那也有点容易:

public int compareTo(Passenger other) {
if (this.isDisability() ^ other.isDisability()) { //use an XOR
return this.isDisability() ? 1 : -1; //1 for us, -1 for other
}
//compare #getClase
int clase = -Integer.compare(this.getClase(), other.getClase()); //invert
if (clase == 0) {
//compare arrival times if clase is equal
//normalize to -1, 1 (0 excluded in OP)
return this.getArrivalTime() < other.getArrivalTime() ? 1 : -1;
}
return clase > 0 ? 1 : -1; //normalize to -1, 0, 1
}

这允许您定义 Passenger 的自然排序。 ,并且被封装/内部到您的类实现中(不需要太多的暴露)。

这也使得排序等操作变得更加容易:

List<Passenger> passengers = /* some list */;
Collections.sort(passengers);

如果您想提供一个可以完成替代排序的比较器,您也可以在您的类中执行此操作:

public class Passenger {

//...

public static class ArrivalComparator implements Comparator<Passenger> {

public int compare(Passenger one, Passenger two) {
return Integer.compare(one.getArrivalTime(), two.getArrivalTime());
}
}

//...

}

使用我们之前的示例,您可以根据到达时间对所有乘客进行排序:

Collections.sort(passengers, new Passenger.ArrivalComparator());

此外,这可以使用 Java 8 内联:

//Sort by arrival time
Collections.sort(passengers, (one, two) -> Integer.compare(one.getArrivalTime(), two.getArrivalTime());

但总的来说,请记住比较器主要用于定义特定的排序,而 Comparable定义一般/自然顺序。

关于java - 如何将比较器与多个字段对象一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562426/

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