排序PriorityQueue [英] Sorting PriorityQueue

查看:134
本文介绍了排序PriorityQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PriorityQueues时遇到问题,因为我认为它是按优先级排序的,但是我不确定优先级是什么(我是说值是什么以及它来自何处).可以使用构造函数中的比较器来创建一个priorityQueue,我已经尝试过了,但是它不起作用.

I am having a problem with PriorityQueues, as I am lead to believe it orders on priority however I am not sure what the priority is (I mean what the value is and where it comes from). A priorityQueue can be made with a comparator in the constructor and I have tried this but it does not work.

队列类:

public JavaPriorityFlightQueue() {
    super();
    flights = new PriorityQueue(5, new SortQueueViaPriority());
}

比较器:

import java.util.Comparator;

public class SortQueueViaPriority implements Comparator {

    public int compare(Object o1, Object o2){
        Flight f1 = (Flight) o1; 
        Flight f2 = (Flight) o2;

        if( f1 == null || f2 == null ){
            if( f1 == f2 ) return 0;
            else if( f2 == null) return +1;
                else return -1;
        }

    Integer i1 = (Integer) f1.getPriority();
    Integer i2 = (Integer) f2.getPriority();
    return i2.compareTo(i1);
    }
}

Priority是一个int值,它是航班等级的一部分.我对此进行测试.

Priority is an int value which is part of the flight class. I test this.

JavaPriorityFlightQueue flightQueue = new JavaPriorityFlightQueue();

Flight flight1 = new Flight("0001",9);
Flight flight2 = new Flight("0002",7);
Flight flight3 = new Flight("0003",1);
Flight flight4 = new Flight("0004",2);
Flight flight5 = new Flight("0005",1);

但是PriorityQueue没有排序,当我检查它时,值9从未与任何东西进行比较,结果是没有排序.比较类SortQueueViaPriority是从另一个类进行完美排序的类中复制并粘贴的.

However the PriorityQueue is not sorted, and when I check it the value 9 is never compared to anything and the result is nothing is sorted. the compare class SortQueueViaPriority is copy and pasted from another class where the class sorts perfectly.

推荐答案

我建议您尝试以下示例.如果将PriorityQueue用作队列,则会按顺序删除条目.

I suggest you try the following example. If you use PriorityQueue as a queue, the entries are removed in order.

import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {
    public static void main(String... args) {
        PriorityQueue<Flight> flights = new PriorityQueue<Flight>(5, new SortQueueViaPriority());
        flights.add(new Flight("0001", 9));
        flights.add(new Flight("0002", 7));
        flights.add(new Flight("0003", 1));
        flights.add(new Flight("0004", 2));
        flights.add(new Flight("0005", 1));

        while (!flights.isEmpty())
            System.out.println(flights.remove());
    }
}

class SortQueueViaPriority implements Comparator<Flight> {
    @Override
    public int compare(Flight f1, Flight f2) {
        return Integer.compare(f2.getPriority(), f1.getPriority());
    }
}

class Flight {
    private final String name;
    private final int priority;

    Flight(String name, int priority) {
        this.name = name;
        this.priority = priority;
    }

    public int getPriority() {
        return priority;
    }

    @Override
    public String toString() {
        return "Flight{" +
                "name='" + name + '\'' +
                ", priority=" + priority +
                '}';
    }
}

打印

Flight{name='0001', priority=9}
Flight{name='0002', priority=7}
Flight{name='0004', priority=2}
Flight{name='0003', priority=1}
Flight{name='0005', priority=1}

注意:PriorityQueue对条目进行排序,以使只有第一个元素最小.如果您遍历队列,将看到所有元素,但它们可能是按顺序排列的,也可能是不按顺序排列的.

Note: PriorityQueue sorts entries such that only the first element will be the smallest. If you iterate over the queue, you will see all the elements, but they may or may not be in order.

这篇关于排序PriorityQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆