Tag: 优先级队列

如何在java中获得比较器的反转

在一个方法中,我收到一个通用object E extends Comparable作为参数。 现在我想创建两个优先级队列。其中一个使用E使用的comparator和其他使用E使用的comparator相反的队列(即如果E使用’ =’)。 请告诉我如何创建两个这样的队列。 queue2=new PriorityQueue(0,Collections.reverseOrder(e)); 我收到reverseOrder不适用的错误。 请帮忙

实现Java优先级队列

public class PriorityQueue { private PriorityNode head, tail; private int numItems; public PriorityQueue(){ numItems = 0; head=null; tail=null; } public void add(int priority, T value){ PriorityNode newNode = new PriorityNode(priority,value); if(numItems == 0){ head = newNode; tail = newNode; } else{ head.setNext(newNode); head = newNode; } } } 其中PriorityNode定义为: public class PriorityNode implements Comparable { […]

在Java中获取k个最小(或最大)数组元素的最快方法是什么?

我有一个元素数组(在这个例子中,这些只是整数),使用一些自定义比较器进行比较。 在这个例子中,我通过定义i SMALLER j模拟这个比较器,当且仅当scores[i] <= scores[j] 。 我有两种方法: 使用当前k候选人的堆 使用当前k候选的数组 我通过以下方式更新上面的两个结构: heap:方法PriorityQueue.poll和PriorityQueue.offer , array:存储候选数组中前k个候选中最差的索引top 。 如果新看到的示例比索引top的元素更好,则后者由前者替换,并且top通过迭代遍历数组的所有k个元素来更新。 但是,当我测试了哪种方法更快时,我发现这是第二种。 问题是: 我对PriorityQueue使用是否不理想? 计算k个最小元素的最快方法是什么? 我感兴趣的是,当例子的数量可以很大,但是邻居的数量相对较小(在10到20之间)。 这是代码: public static void main(String[] args) { long kopica, navadno, sortiranje; int numTries = 10000; int numExamples = 1000; int numNeighbours = 10; navadno = testSimple(numExamples, numNeighbours, numTries); kopica = testHeap(numExamples, numNeighbours, numTries); sortiranje […]

删除PriorityQueue的顶部?

假设我正在使用Java.util中的PriorityQueue类。 我想从PriorityQueue pq中删除最大的数字,我们假设它位于队列的头部。 以下工作会怎样? // 1 int head = pq.peek(); pq.dequeue(head); // 2 int head = pq.dequeue(pq.peek()); 对于非原始人来说,它的工作原理是否相同?

Java PriorityQueue Comparator – 如何/何时排序?

我正在初始化一个优先级队列,如: strategy = new FuelPriority(); incoming = new PriorityQueue(1, strategy); 我的Comparator类的代码是: public class FuelPriority implements Comparator { public int compare(Object o1, Object o2) { Vehicle a1 = (Vehicle) o1; Vehicle a2 = (Vheicle) o2; return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel()); } } 运行模拟后,元素根本没有排序 – 它们是随机的; 我在FuelPriority类的compare方法中设置了一个断点,但根本没有调用它。 我在这里错过了什么吗?

在Java中排序优先级队列

我试图在PriorityQueue插入整数,我知道: 如果在构造PriorityQueue时未指定比较器,则使用存储在队列中的数据类型的默认比较器。 默认比较器将按升序对队列进行排序 但是,我得到的输出不是按排序顺序。 运行以下代码后的输出为: [2, 4, 8, 6] public static void main(String args[]) { PriorityQueue q = new PriorityQueue(10); q.offer(4); q.offer(2); q.offer(8); q.offer(6); System.out.print(q); } 有人可以解释一下原因吗?

为什么Java中的PriorityBlockingQueue没有正确排序?

出于某种原因,当我添加到优先级队列时,它不会完全按字母顺序排序我的字符串,我不明白为什么。 这是添加到PriorityBlockingQueue的代码: String toAdd = String.format(“%s/%s”, directory, s); outputData.add(toAdd); 但我没有完全排序输出(只有前几行,但你可以看到它没有排序): ../StartingTree/files/abknl/apfmpohgyh/a.class ../StartingTree/files/abknl/apfmpohgyh/a.java ../StartingTree/files/abknl/aqybc/aeph.java ../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.class ../StartingTree/files/abknl/bbxudleuf/jlffhq/y/xwjj/dyetqhsch/bpg.class ../StartingTree/files/abknl/bbxudleuf/mxb/fe/ndmg/axapxuco.html ../StartingTree/files/abknl/aqybc/atyuojdu.txt 这是预期输出文件的排序输出的真实(第一部分): ../StartingTree/files/abknl/apfmpohgyh/a.class ../StartingTree/files/abknl/apfmpohgyh/a.java ../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.class ../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.java ../StartingTree/files/abknl/apfmpohgyh/bsqsq.class ../StartingTree/files/abknl/apfmpohgyh/bsqsq.java ../StartingTree/files/abknl/apfmpohgyh/ds.class ../StartingTree/files/abknl/apfmpohgyh/ds.java

通过PriorityQueue迭代不会产生有序结果

import java.util.*; class Priority{ public static void main(String args[]){ PriorityQueue queue=new PriorityQueue(); queue.add(“Amit”); queue.add(“Vijay”); queue.add(“Karan”); queue.add(“Jai”); queue.add(“Rahul”); System.out.println(“head:”+queue.element()); System.out.println(“head:”+queue.peek()); System.out.println(“iterating the queue elements:”); Iterator itr=queue.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } queue.remove(); queue.poll(); System.out.println(“after removing two elements:”); Iterator itr2=queue.iterator(); while(itr2.hasNext()){ System.out.println(itr2.next()); } } } Output:head:Amit head:Amit iterating the queue elements: Amit Jai Karan Vijay Rahul after removing two […]

打印优先级队列的内容

如何让print_queue在java中正常工作? 这是我自己的队列实现。 使用Iterator()工作正常,除了它以随机顺序打印数字。 package data_structures_java ; import java.util.Iterator; import java.util.PriorityQueue ; import java.util.* ; public class Queue_implementation { PriorityQueue actual_queue ; public Queue_implementation(){ actual_queue = new PriorityQueue() ; } public void add(int num){ actual_queue.add(num) ; } public int remove(){ return actual_queue.remove() ; } public int peek(){ if( actual_queue.isEmpty()) return -1 ; else return actual_queue.peek() ; […]

队列没有自然排序

可能重复: 为什么这个奇怪的顺序发生在java的PriorityQueue中? 请看下面的代码: public static void main(String[] args) { Queue q = new PriorityQueue(); q.offer(“car”); q.offer(“airplane”); q.offer(“bicycle”); Iterator i = q.iterator(); while(i.hasNext()) System.out.print(i.next() + ” “); } 有人可以解释为什么输出 airplane car bicycle 代替 airplane bicycle car ? 因为在API中它表示优先级队列的元素是按照它们的自然顺序排序的。