13

in Java, I don't know how to create a new PriorityQueue with new comparator but without given the queue length? How can I create it?

I know I can write:

Queue<Node> theQueue = new PriorityQueue<Node>(15,new Comparator<Node>();

But I hope the queue can works like LinkedList, I mean its length is not fixed, how can I declare it?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
lkkeepmoving
  • 2,323
  • 5
  • 25
  • 31

4 Answers4

11

Modern answer, as of 2021: https://stackoverflow.com/a/30015986/139010


Pre-Java-8 answer, for posterity:

There is no such constructor. As per the JavaDocs, the default capacity is 11, so you could specify that for analogous behavior to the no-arg PriorityQueue constructor:

Queue<Node> theQueue = new PriorityQueue<Node>(11,new Comparator<Node>());

And yes, the queue will grow if it needs to.

A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.x

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Got it. And another question: if I want to do local beam search, I need to limit the PriorityQueue to a fix number (for example: 10), how can I fix the length of a PriorityQueue? – lkkeepmoving Feb 26 '13 at 20:44
  • Use a size-bounded [`LinkedBlockingQueue`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html). – Matt Ball Feb 26 '13 at 20:48
  • But the LinkedBlockingQueue class works differently from PriorityQueue, right? Can a queue have the feature of both LinkedBlockingQueue and PriorityQueue? – lkkeepmoving Feb 26 '13 at 20:54
  • Java 8 offers a constructor to do what is asked in the question. – Naveen Kumar Jun 25 '21 at 07:52
  • @NaveenKumar thanks for the info. Do you mind posting a link to the JavaDocs for this? – Matt Ball Jun 26 '21 at 15:39
  • 1
    @MattBall Sure. Here it is: https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html#PriorityQueue-java.util.Comparator- Joris Kinable has already added it as an answer: https://stackoverflow.com/a/30015986/6038386 – Naveen Kumar Jun 26 '21 at 18:23
  • Thanks! I've edited my answer to point to Joris' as the more-relevant one. – Matt Ball Jun 26 '21 at 19:32
5

Starting from Java version 8 there's a new constructor which can do what you ask for: PriorityQueue(Comparator comparator)

So you would get:

Queue<Node> theQueue = new PriorityQueue<>(new Comparator<Node>());
Joris Kinable
  • 2,232
  • 18
  • 29
  • 1
    From doc: `Creates a PriorityQueue with the default initial capacity and whose elements are ordered according to the specified comparator.` So, it still 11 xD – Enissay Mar 08 '16 at 14:29
1

I'm afraid theres no way to specify only a Comparator without specifying an initial capacity. Note that this is only the initial capacity - the queue can grow from this initial value.

Sean Landsman
  • 7,033
  • 2
  • 29
  • 33
0

You can create a priority queue with self-defined comparator without fixing the size by using Java Lambda, a feature in Java SE 8.

For example, you can do:

PriorityQueue<String> pq = new PriorityQueue<>((s1, s2) -> s1.compareTo(s2));

See example about Lambda: https://www.mkyong.com/java8/java-8-lambda-comparator-example/