java 中 priorityqueue 的比较器

comparator of priorityqueue in java

我正在尝试使用 priorityqueue 的覆盖比较器方法,我想实现以下目标:

我有当前列表:

RG3

PR1

PR2

RG4

RG1

RG2

RG代表普通人,PR代表优先人,数字代表轮次。 我想要的是获得一个先进先出的顺序,除非优先顺序是轮到队列顶部的人。所以在列表中我想要以下结果

PR1

PR2

RG1

RG2

RG3

RG4

这是我到目前为止所做的:

Queue<Ficha> cola = new PriorityQueue<>(6, idComparator);


while (!list.isEmpty()) //this list is the unsorted list.
         {
             aux = list.remove(0);

             cola.add(aux); // adds it to the priority queue

         }

         while(!cola.isEmpty())
         {
             aux = cola.poll();
             System.out.println(aux.getCod_priority()+aux.getTurn()); // this shows me the order of the queue
         }


    }

    public static Comparator<Ficha> idComparator = new Comparator<Ficha>()
    {

        @Override
        public int compare(Ficha f1, Ficha f2) {
            return (int) ((f1.getTurn()+prioridad(f1.getCod_priority())) - (f2.getTurn()+prioridad(f2.getCod_priority())));
       }
    };


    private static long prioridad(String cod_priority) // this method i use it to give the cod_priority a int value to compare 
    {
        if(cod_tipo_ficha=="PR")
        {
            return 10000;
        }
        else
        {
            return 1;
        }
    }

当我 运行 它时,我得到以下命令:

PR1

RG1

RG2

PR2

RG3

RG4

我知道我的问题出在比较器方法中,但我不知道如何实现我想要的队列。

我知道有很多关于如何比较的问题,但我看到的唯一答案是当你比较字符串时。而这个我需要比较优先级字符串和整数。

只要把cod_tipo_ficha=="PR"改成

if("PR".equals(cod_tipo_ficha)) {
    ...
}

应该可以