如何在Java中定义基于两个变量进行比较的Comparator

时间:2011-03-29 08:32:54

标签: java collections

我想创建一个比较器进行操作,使得到达时间较短的进程将首先出现在排序中,如果两个进程具有相同的到达时间,那么具有较低进程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());
    }
}

具体而言,给定以下流程

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

我在最后得到以下订购

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

4 个答案:

答案 0 :(得分:4)

我发现你的比较器没有任何问题。这是我的测试用例:

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

    }
}

输出:

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]

答案 1 :(得分:3)

我假设您正在比较的事情是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;
        }
    }
}

编辑:我检查了上面的代码,它确实输出了正确的顺序。我只能假设您的代码中有其他地方存在错误。

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

完整的测试代码是:

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

答案 2 :(得分:2)

我相信你想要这个:

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

答案 3 :(得分:1)

最简单的是:

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