如何在 Java 中定义基于两个变量进行比较的比较器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5469830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a Comparator in Java that compares based on two variables
提问by Varun Madiath
I want to create a comparator to operate such that a process with a lower arrival time will appear first in a sorting, and if two processes have the same arrival time, the one with the lower process id comes first in the sorting. I tried the following code, but it doesn't seem to be working. Does anyone see a flaw in it?
我想创建一个比较器来操作,以便具有较低到达时间的进程将首先出现在排序中,如果两个进程具有相同的到达时间,则具有较低进程 id 的进程首先出现在排序中。我尝试了以下代码,但似乎不起作用。有没有人看到它的缺陷?
public class FCFSComparator implements Comparator<Process>
{
public int compare(Process o1, Process o2)
{
int result = o1.getArrivalTime() - o2.getArrivalTime();
if(result == 0)
{
return (o1.getPid() < o2.getPid()) ? -1 : 1;
}
else
{
return result;
}
// return (result != 0 ? result : o1.getPid() - o2.getPid());
}
}
To be specific, given the processes as follows
具体来说,给定过程如下
pid = 0 arrival time = 10
pid = 1 arrival time = 30
pid = 2 arrival time = 15
pid = 3 arrival time = 15
pid = 4 arrival time = 66
I get the following ordering at the end
我在最后得到以下顺序
Pid = 0 arrival time = 10
Pid = 2 arrival time = 15
Pid = 1 arrival time = 30
Pid = 4 arrival time = 66
Pid = 3 arrival time = 15
回答by bruno conde
I can't find anything wrong with your comparator. Here is my test case:
我找不到你的比较器有什么问题。这是我的测试用例:
public class TestComparator {
static class Process {
int pid;
int arrivalTime;
Process(int pid, int arrivalTime) {
this.pid = pid;
this.arrivalTime = arrivalTime;
}
@Override
public String toString() {
return "Process [pid=" + pid + ", arrivalTime=" + arrivalTime + "]";
}
}
static class FCFSComparator implements Comparator<Process> {
public int compare(Process o1, Process o2) {
int result = o1.arrivalTime - o2.arrivalTime;
if (result == 0) {
return (o1.pid < o2.pid) ? -1 : 1;
} else {
return result;
}
}
}
public static void main(String[] args) {
List<Process> processes = Arrays.asList(
new Process(0, 10),
new Process(1, 30),
new Process(2, 15),
new Process(3, 15),
new Process(4, 66));
Collections.sort(processes, new FCFSComparator());
for (Process process : processes) {
System.out.println(process);
}
}
}
Output:
输出:
Process [pid=0, arrivalTime=10]
Process [pid=2, arrivalTime=15]
Process [pid=3, arrivalTime=15]
Process [pid=1, arrivalTime=30]
Process [pid=4, arrivalTime=66]
回答by Jon
I'm making that assumption that the things you are comparing are int
. In the case of both variables being equal, you are still returning 1 from the inner comparison. Something like this should help:
我假设你正在比较的东西是int
. 在两个变量相等的情况下,您仍然从内部比较中返回 1。这样的事情应该有帮助:
public class FCFSComparator implements Comparator<Process>
{
public int compare(Process o1, Process o2)
{
int result = o1.getArrivalTime() - o2.getArrivalTime();
if (result == 0)
{
return o1.getPid() - o2.getPid();
}
else
{
return result;
}
}
}
EDIT: I checked the above code and it does output the correct order. I can only assume you have a bug somewhere else in your code.
编辑:我检查了上面的代码,它确实输出了正确的顺序。我只能假设您的代码中的其他地方有错误。
Pid = 0 arrival time = 10
Pid = 2 arrival time = 15
Pid = 3 arrival time = 15
Pid = 1 arrival time = 30
Pid = 4 arrival time = 66
The full test code is:
完整的测试代码是:
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String[] args)
{
List<Process> processes = new ArrayList<Process>();
processes.add(new Process(10, 0));
processes.add(new Process(30, 1));
processes.add(new Process(15, 2));
processes.add(new Process(15, 3));
processes.add(new Process(66, 4));
Collections.sort(processes, new FCFSComparator());
for (Process process : processes)
System.out.println("Pid = " + process.getPid() + " arrival time = " + process.getArrivalTime());
}
static class FCFSComparator implements Comparator<Process>
{
public int compare(Process o1, Process o2)
{
int result = o1.getArrivalTime() - o2.getArrivalTime();
if (result == 0)
{
return o1.getPid() - o2.getPid();
}
else
{
return result;
}
}
}
static class Process
{
private int arrivalTime;
private int pid;
Process(int arrivalTime, int pid)
{
this.arrivalTime = arrivalTime;
this.pid = pid;
}
public int getArrivalTime()
{
return arrivalTime;
}
public int getPid()
{
return pid;
}
}
}
回答by WhiteFang34
I believe you want this:
我相信你想要这个:
public class FCFSComparator implements Comparator<Process> {
public int compare(Process o1, Process o2) {
if (o1.getArrivalTime() == o2.getArrivalTime()) {
return (o1.getPid() < o2.getPid()) ? -1 : 1;
}
return (o1.getArrivalTime() < o2.getArrivalTime()) ? -1 : 1;
}
}
回答by mmjmanders
The simplest is:
最简单的是:
public class FCFSComparator implements Comparator<Process>
{
public int compare(Process o1, Process o2)
{
int timeCmp = Integer.valueOf(o1.getArrivalTime()).compareTo(Integer.valueOf(o2.getArrivalTime()));
return (timeCmp != 0 ? timeCmp : Integer.valueOf(o1.getPid()).compareTo(Integer.valueOf(o2.getPid())));
}
}