Enqueue/ Dequeue OR offer/ poll

10,181

I am assuming that "PQ" means "priority queue". I've never used such a queue (my mental image of a queue is that of a strictly FIFO structure), but after reading documentation, I think you can do this:

First, you need to create the class of the object you want to store in the queue. Assuming int contents and int priority:

public class MyClass implements Comparable<MyClass> {
    private int x, p;

    /*
     * x: Contents
     * p: Priority
     */
    public MyClass(int x, int p) {
        this.x = x;
        this.p = p;
    }

    @override
    public int compareTo(MyClass o) {
        return this.p - o.p;
    }

    public int getX() {
        return x;
    }
}

Now, create your priority queue. If I understood correctly the class documentation, it will use the compareTo method to sort your objects:

....
PriorityQueue<MyClass> pq = new PriorityQueue<MyClass>();
....
pq.add(new MyClass(x, p));
....

Check: http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

Java queues don't have enqueue and dequeue methods; these operations are done using the following methods:

  • Enqueuing:
    • add(e): throws exception if it fails to insert the object
    • offer(e): returns false if it fails to insert the object
  • Dequeuing:
    • remove(): throws exception if the queue is empty
    • poll(): returns null if the queue is empty
  • Take a look to the first object in the queue:
    • element(): throws exception if the queue is empty
    • peek(): returns null if the queue is empty

And now, finally: when to use offer and add?

About offer and add: Well, that depends on how do you want to handle the failure of the insertion in the queue:

The add method, which Queue inherits from Collection, inserts an element unless it would violate the queue's capacity restrictions, in which case it throws IllegalStateException. The offer method, which is intended solely for use on bounded queues, differs from add only in that it indicates failure to insert an element by returning false.

(see: http://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html)

Hope this helps you

Share:
10,181
InspiringProgramming
Author by

InspiringProgramming

Updated on November 23, 2022

Comments

  • InspiringProgramming
    InspiringProgramming over 1 year

    This seems like a very basic question, but I've been stuck for hours.

    When do we use methods enqueue/ dequeue & when do we use offer/ poll?!

    I want to create a PQ of integers with the void enqueue(int x, int p) and int dequeue() methods, how do I declare such a queue?

    Thanks.

    • JB Nizet
      JB Nizet over 11 years
      Which queue are you talking about? The standard java.util.Queue doesn't have any enqueue/dequeue method.
    • trashgod
      trashgod over 11 years
      See Queue<E> for details on clarifying your question.
  • InspiringProgramming
    InspiringProgramming over 11 years
    Thanks, it helped me GREATLY. I appreciate it.
  • JB Nizet
    JB Nizet over 11 years
    @InspiringProgramming: then you should accept the answer and upvote it.
  • InspiringProgramming
    InspiringProgramming over 11 years
    I'm still not allowed to vote because my reputation is less than 15 :( I wish I could.
  • Barranka
    Barranka over 11 years
    @InspiringProgramming Happy to help. Just one question: are you using bounded queues? if you are not using them, then you won't be able (or need) to use offer ;-)