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

发布于 2024-10-27 18:48:30 字数 911 浏览 0 评论 0 原文

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法唧唧 2024-11-03 18:48:30

我找不到你的比较器有什么问题。这是我的测试用例:

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]

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]
万水千山粽是情ミ 2024-11-03 18:48:30

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

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:

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;
        }
    }
}
傲世九天 2024-11-03 18:48:30

我相信你想要这个:

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

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;
    }
}
寻找一个思念的角度 2024-11-03 18:48:30

最简单的是:

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

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())));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文